SeyoS SeyoS - 1 year ago 62
SQL Question

Is the order of selection parameters important?

If i use a table containing at least 2 columns like this :


Assuming that there is a huge amount of data and that there is much more unique result in 'FirstName' than in 'LastName', which query is the fastest :

Query 1 :

SELECT * USER WHERE LastName = 'Dupont' AND FirstName = 'Robert'

Query 2:

SELECT * USER WHERE FirstName = 'Robert' AND LastName = 'Dupont'

Is there a difference ?

Thanks for your help !

Answer Source

The order of the comparisons does not matter. SQL Server should optimize the WHERE clause for the data.

However, if you care about performance, you should use an index, either user(firstname, lastname) or user(lastname, firstname). The order of comparisons is not the right thing to be thinking about.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download