-3

Possible Duplicate:
Does the order of columns in a WHERE clause matter?

These are the basics SQL Function and Keywords.

Is there any tips or trick to speed up your SQL ?

For example; I have a query with a lot of keywords. (AND, GROUP BY, ORDER BY, IN, BETWEEN, LIKE... etc.)

Which Keyword should be on top in my query? How can i decide it?

Example;

Where NUMBER IN (156, 646)
AND DATE BETWEEN '01/01/2011' AND '01/02/2011'

OR

Where DATE BETWEEN '01/01/2011' AND '01/02/2011'
AND NUMBER IN (156, 646)

Which one is faster? Depends of what?

12
  • Don't use joins (denormalize your data) , use indexes ;). Mar 28, 2011 at 8:42
  • The SQL optimizer will generate the same plan for both your example queries. This is not something you need to worry about.
    – Blorgbeard
    Mar 28, 2011 at 8:44
  • 1
    @Bugai13 Why indexes is better joins? Can you explain? NOTE: Why this post should close? I don't understand. Mar 28, 2011 at 8:45
  • @Soner: i mean don't use joins and use indexes, but not that indexes better than joins. And also don't see reasons to close post. Mar 28, 2011 at 8:52
  • 3
    Not all updates happen asynch from applications. And trust me the user notices when the customer changes his name and 10,million records need to get updated in one transaction.
    – HLGEM
    Mar 28, 2011 at 21:23

4 Answers 4

5

Don't use functions in the where clause. Because the query engine must execute the function for every single row.

3
  • I see no problem with WHERE my_primary_key = my_neural_network_function('a', 'b', 'c')
    – Ronnis
    Mar 28, 2011 at 13:40
  • Its the fact that it has to run my_neural_network_function('a', 'b', 'c') each and every time stops the query from being optimised Mar 28, 2011 at 13:53
  • @Ronnis did you evalute for your query with WHERE my_primary_key = my_neural_network_function('a', 'b', 'c') ? Is query take long? Mar 28, 2011 at 18:58
4

There are no "tricks".

Given the competition between the database vendors about which one is "faster", any "trick" that is always true would be implemented in the database itself. (The tricks are implemented in the part of the database called "optimizer").

There are only things to be aware of, but they typically can't be reduced into:

  • Use feature X
  • Avoid feature Y
  • Model like this
  • Never model like that

Look at all the raging questions/discussions about indexes, index types, index strategies, clustering, single column keys, compound keys, referential integrity, access paths, joins, join mechanisms, storage engines, optimizer behaviour, datatypes, normalization, query transformations, denormalization, procedures, buffer cache, resultset cache, application cache, modeling, aggregation, functions, views, indexed views, set processing, procedural processing and the list goes on.

All of them was invented to attack a specific problem area. Variations on that problem make the "trick" more or less suitable. Very often the tricks have zero effect, and sometimes sometimes flat out horrible. Why? Because when we don't understand why something works, we are basically just throwing features at the problem until it goes away.

The key point here is that there is a reason why something makes a query go faster, and the understanding of what that something is, is crucial to the process of understanding why a different unrelated query is slow, and how to deal with it. And it is never a trick, nor magic.

We (humans) are lazy, and we want to be thrown that fish when what we really need is to learn how to catch it.

Now, what specific fish do YOU want to catch?

Edited for comments:
The placement of your predicates in the where clause makes no difference since the order in which they are processed is determined by the database. Some of the things which will affect that order (for your example) are :

  • Whether or not the query can be rewritten against an indexed view
  • What indexes are available that covers one or both of columns NUMBER and DATE and in what order they exist in that index
  • The estimated selectivity of your predicates, which basically mean the estimated percentage of rows matched by your predicate. The lower % the more likely the optimizer is to use your index efficiently.
  • The clustering factor (or whatever the name is in SQL Server) if SQL Server factors that into the query cost. This has to do with how the order of the index entries aligns with the physical order of the table rows. Better alignment = reduces cost for higher % of rows fetched via that index.

Now, if the only values you have in column NUMBER are 156, 646 and they are pretty much evenly spread, an index would be useless. A full scan would be a better alternative.
On the other hand, if those are unique order numbers (backed by a unique index), the optimizer will pick that index and drive the query from there. Similarily, if the rows having a DATE between the first and second of January 2011 make up a small enough % of the rows, an index leading with DATE will be considered.

Or if you include order by NUMBER, DATE another parameter comes into the equation; the cost of sorting. An index on (NUMBER, DATE) will now seem more attractive to the optimizer, because even though it might not be the most efficient way of aquiring the rows, the sorting (which is expensive) can be skipped.

Or, if your query included a join to another table (say customer) on customer_id and you also had a filter on customer.ssn, again the equation changes, because (since you did a good job with foreign keys and a backing index) you will now have a very efficient access path into your first table, without using the indexes in NUMBER or DATE. Unless you only have one customer and all of the 10 million orders where his...

2
  • Thanks! Yes it varies with the individual. But i want to learn for exampe; in WHERE clause IN, BETWEEN, NULL, LIKE.. etc. Which one is should be on the top of query? I depends on my data i know that but Can we make generalizations for this rules? Mar 28, 2011 at 19:05
  • 1
    @Soner, no. SQL is declarative, and the database is free to rewrite your query in any logically equivalent way. Which is what I mean by The placement of your predicates in the where clause makes no difference since the order in which they are processed is determined by the database
    – Ronnis
    Mar 30, 2011 at 7:29
3

Read about sargable queries (ones which can use the index vice ones which can't). Avoid correlated subqueries, functions in where clauses, cursors and while loops. Don't use select * especially if you have joins, never return more than the data you need.

Actually there are whole books written on performance tuning, get one and read it for the datbase you are using as the techniques vary from database to database.

4
  • Thanks! What do you thins using index or not using? And what is wrong using cursors in while loop? Mar 28, 2011 at 19:01
  • 1
    You should use indexes but need to be careful of them (they speed up selects and slow down inserts/updates/deletes so you only want the ones you are actually going to need) and cursors and while loop run row by row and are very slow. I have fixed cursors that were taking 45 minutes to insert records and replaced with proper set-based code that took seconds and fixed looping processes that took over 24 hours down to less than an hour. Cursors and loops are the technique of last resort never the first technique you pick.
    – HLGEM
    Mar 28, 2011 at 19:07
  • Nice one. As i understand, i should use indexes in select part but shouldn't use with inserts/update/delete. By the way, SQL is working from top to bottom right? So, if i have less conditions in my WHERE clause, this clause should be top of the WHERE clause righT? For example; A = 100 (geting 1.000.000 row) and B=100 (getting 10 row) should A=100 under the B=100 right? Mar 30, 2011 at 7:20
  • 1
    @Soner SQL does NOT work top to bottom. It processes things in an indeterminate order based on its decision about what will be the most efficient access path to produce the requested logical set of data.
    – ErikE
    Jul 14, 2011 at 16:33
2

Learn to use indexes properly.

http://Use-The-Index-Luke.com/

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.