Please help me with SQL query optimization

Subhomoy Chakraborty 106 Reputation points
2022-04-11T10:15:33.22+00:00

Hi Team,

Please find the attached query. It is giving around 86M+ records as output. But while I am using Order By clause the query it getting stuck and even for top 100 records it is taking time. Please suggest.

191883-capture.jpg

Developer technologies Transact-SQL
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2022-04-11T10:21:02.6+00:00

    Please find the attached query.

    There is no query, only a photo of something which might could be a SQL query.

    That's to less on information.
    Please post table design as DDL, some sample data as DML statement and the expected result.

    0 comments No comments

  2. Subhomoy Chakraborty 106 Reputation points
    2022-04-11T12:31:50.87+00:00

    SQL Query:
    SELECT
    concat(ep.endpoint_id,ep.version_number,client_received_start_timestamp,response_status_code) as u
    ,ep.endpoint_id
    ,ep.version_number
    ,client_received_start_timestamp
    ,client_received_start_date
    from table1 ap
    left outer join table2 ep on ap.api_endpoint=ep.api_endpoint
    order by ep.endpoint_id,ep.version_number,client_received_start_timestamp

    0 comments No comments

  3. Naomi Nosonovsky 8,431 Reputation points
    2022-04-11T12:32:24.503+00:00

    Since you're selecting into a temp table, there is no point in using ORDER BY (unless you want only a portion of records). In the latter case you may try to use ROW_NUMBER() approach and CTE to select rows you need.

    0 comments No comments

  4. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-04-11T21:52:00.09+00:00

    Sorting 86 million rows will take quite some time. The only way to avoid that is to have indexes that support the ORDER BY clause.

    Although, insult could be added to injury, if SQL Server misestimates the number of rows, and think that only, say, a million rows will be returned, since the memory grant will be too small, leading to serious sort spill.

    We would need to see the estimated execution plan. You can upload it on http://www.pastetheplan.com. Even better would be the actual execution plan, since the query does not complete, I guess we will have to be without it.

    It would also help to have the CREATE TABLE and CREATE INDEX statements for the two tables.

    0 comments No comments

  5. Bert Zhou-msft 3,436 Reputation points
    2022-04-12T03:05:47.667+00:00

    Hi,@Subhomoy Chakraborty

    Welcome to Microsoft T-SQL Q&A Forum!

    1->order by is useless as part of select into#temp (no identification, no clustered index), by adding an index to the temporary table, and then adding it after the data is loaded into the table;
    2->Put the code into the CTE, you can try to create an index on the api_endpoint column in the CTE:

    CREATE CLUSTERED INDEX AP_Index ON #TEMP1(api_endpoint);  
    

    I suggest you refer to this article.
    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.