Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will an SQL statement stop execution on the first match in an OR statement?

Tags:

sql

mysql

I'm trying to understand how the following statement will execute on multiple records.

SELECT * FROM test 
WHERE product='$product' 
AND (summary LIKE '%$keywords%' OR exp LIKE '%$keywords%')  
ORDER BY `$order_by` DESC;

I'm fairly new to SQL so here's what I'd like to know:

1. When you execute a SQL query, I believe it looks at each row in the table. When a match is found, where does it temporarily store the result until all results can be returned?

2. In the above example, say we're looking at single row in the table and there is a match for summary. Will the query continue to execute OR exp LIKE '%$keywords%' when its already found a match?

like image 937
kaizenCoder Avatar asked Mar 19 '13 01:03

kaizenCoder


Video Answer


2 Answers

  1. It's up to the database engine to decide how to store the temporary result. Each database engine implementation might be different. What the client get is a result set consisting of rows (if the query produces result at all)

  2. No, if the first condition in the OR evaluates to true, it will not check the second condition (what's the point of doing that anyway, you know the end result already)

like image 98
gerrytan Avatar answered Nov 09 '22 22:11

gerrytan


  1. In memory, depending on the DBMS.
  2. when it finds a match it will probably not continue. Everything depends on the particular SQL implementation, but in general it wouldn't be efficient to carry on looking for matches, so I would say probably not.
like image 29
tjb1982 Avatar answered Nov 09 '22 20:11

tjb1982