Quantcast
Viewing all articles
Browse latest Browse all 8

Help Optimizing TSQL Query

Hello,

I don't have very many peers at my office to raise my TSQL skills so i'm turning here again. I have a query below that I wrote and it works but is there a better way. I also considered using a CTE...would that have been better or is this one of those do it both ways and check the stats sort of deals. I can post the code for the table if needed but it's pretty basic.

I get passed in a random number that has been selected from the total row count of the table. I then find the ParentLoginID and select all records with that ParentID. Usually there are three as a user is setup with 3 MFA questions. Currently I generate the random number with a seperate Count(*) query and then let the C# app calculate the random number. I then make a second call with that random number. I must admit that the RAND() in TSQL confuses me so I went this route.As a bonus I would love to do it all in one SP call.

Here is the query:

DECLARE @RndmNum intSET @RndmNum = 2 SELECT
   vw_FulTbl.[ParentLoginID],
   vw_FulTbl.[LoginName],
   vw_FulTbl.[LoginPwd],
   vw_FulTbl.[LoginSSN],
   vw_FulTbl.[MultiFactorQuestion],
   vw_FulTbl.[MultiFactorAnswer] FROM [MMA_TestCases].[dbo].[v_CompleteLogin] AS vw_FulTblINNERJOIN
(  SELECTROW_NUMBER() OVER (ORDERBY ParentLoginID ASC) AS RowNumber,
      [ParentLoginID],
      [LoginName],
      [LoginPwd],
      [LoginSSN],
      [MultiFactorQuestion],
      [MultiFactorAnswer] FROM [MMA_TestCases].[dbo].[v_CompleteLogin]
) AS inVw_RecrdsON vw_FulTbl.ParentLoginID = inVw_Recrds.ParentLoginIDWHERE inVw_Recrds.RowNumber = @RndmNum
TIA
JB

Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>