0

I have a query like

Select * from tbl1 
WHERE 
 ( price > 10 
AND qty > 200 
AND condition=1 
AND name like '%abc%') 

My question is how MySQL will process this? My concern is that I want to run query in this way that results should be reduced on basis of conditions.

For example

( price > 10 AND qty > 200 AND condition=1 And name like '%abc%') 

First, MySQL should filter records having price > 10 and apply next condtion qty > 200 on resultant dataset and at the end, it should put this condition AND name like '%abc%' on it.

How should I write query to achieve this?

Another question is: Does MySQL start reading conditions from start to end or from end to start?

ypercubeTM
99.7k13 gold badges217 silver badges306 bronze badges
asked May 31, 2013 at 8:26
1
  • Basically, SQL is a declarative language. You tell what you want, the server figures out how best to achieve that. From what I've learnt this far, you only need to start worrying if the performance is really bad. Commented May 31, 2013 at 9:50

2 Answers 2

2

How MySQL processes the query largely (and primarily) depends on the indexes on the table.

If no indexes exist, then of course a full table scan is required. Now, you are looking for a way to reduce the search space based on conditions. However:

  • Your query uses one "equals" condition
  • Your query uses two "range" conditions
  • It uses one LIKE clause with a prefix '%'

The LIKE clause, beginning with '%' negates any use of an index, unless it is a covering index, which is not the case in your query. This part of the clause negates any use of an index on the name column.

Well, you can use as many "equals" conditions as you like, where possible, but up to one range condition only, when using an index.

Any index you'll want to put on that table will start with the condition column, since it is the one column where you say condition = 1, which is an equality check. You are then left to choose any of the two columns on which you place a range condition. Choose the one which will most likely reduce more rows (leaving less rows in the search space).

So, you options are:

  • KEY (condition, qty)
  • KEY (condition, price)

So that MySQL will first match the condition column in the index, then follow up to the next column, whichever it is.

MySQL does not read conditions from start to end nor from end to start. It just notes down the various conditions, then tries, when possible, to find an index which satisfies them (or part of them). It then depends on the order of columns in the index -- not on the order by which the columns appear in the query.

answered May 31, 2013 at 12:06
0

If you do not have index on table, then mysql will check every row for all conditions and that will be slow. If you add correct index, you can reduce number of rows that it has to check. But you should avoid

like '%abc%'

search - it is unable to use any index. Use full text search or external search engines like Sorl, Elasticsearch or Sphinx for extensive text search.

answered May 31, 2013 at 10:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.