On 18 Aug 2009, at 10:26pm, yaconsult wrote:

> My question is if it makes a difference to use parentheses when not
> logically required, as in the following example where the only  
> logical used
> is "and":
>
>            select * from log
>            where
>              (response >= 200 and response < 300)
>              and
>              (port >= 8444 and port <= 8459)
>
> Is there any difference in execution or performance if the  
> parentheses are
> present or not?  I'm guessing not, but would like confirmation.
>
>            select * from log
>            where
>              response >= 200
>              and response < 300
>              and port >= 8444
>              and port <= 8459

These would, as you suspect, have different results when seen as  
SQLite queries.  One of them tests every record against two pieces of  
logic, the other uses four constraints each one of which might be  
implemented by clever use of an index.  You might want to use EXPLAIN  
on them to see what each one does.

But I mostly wanted to note that neither of these are the best way to  
do it.  You would probably be best using the BETWEEN operator:

<http://sqlite.org/lang_expr.html>

            SELECT * FROM log
            WHERE response BETWEEN 200 AND 299
              AND port BETWEEN 8444 AND 8458

Note that I had to change two of the integers because it tests for  
'<=' not '='.  (I keep forgetting to do this in my own programming.)   
This format allows the query optimiser to make the best possible use  
of the constraints:

<http://sqlite.org/optoverview.html>

Simon.
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to