how to run the sql query faster?

Fastbest 40 Reputation points
2023-08-31T14:03:12.1166667+00:00

Hi guys!

I cannot find the tag for t-sql? Hope the tag is correct to get help.

May i know how to run the sql query faster?

Sorry that i cannot share my code. I'm only consult two tables and do inner join, why it takes several minutes to show the result?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,494 questions
0 comments No comments
{count} votes

Accepted answer
  1. PercyTang-MSFT 12,511 Reputation points Microsoft External Staff
    2023-09-01T08:44:40.35+00:00

    Hi @Fastbest

    Your query statement is not complicated. The reason for the long running time is that the data of the two tables is too large.

    As a supplement, you can refer to this document to learn the index.

    https://learn.microsoft.com/en-us/sql/t-sql/statements/create-index-transact-sql?view=sql-server-ver16

    Best regards,

    Percy Tang

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Zahid Butt 961 Reputation points
    2023-08-31T15:04:55.0566667+00:00

    Hi Fastbest,

    Its better if you give some detail so experts may help you in a better way.

    Generally speaking, if your tables have large amounts of data then you should have indexes on them to optimize queries.

    e.g. if you have inner join on 2 columns i.e id ,name then you should create index on id and name.

    Hope it helps.

    0 comments No comments

  2. Bruce (SqlWork.com) 74,851 Reputation points
    2023-08-31T20:31:51.81+00:00

    the only way to make a two table join faster is with better indexes. if you look at the query plan, you want an index seek for the first table, and merge join for the second table (with no sort), if low number of result rows a nested index lookup on the second table may be ok depending on the filter.

    if the you have any table scans, you need to remove (unless the table is < 1000 rows).

    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.