Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

efficiency using or within an sql statement on mysql

 
   Database Help (Home) -> mySQL RSS
Next:  Need help with Sybase Perl-DBI warning  
Author Message
Bill Karwin1

External


Since: Jun 17, 2004
Posts: 42



(Msg. 1) Posted: Mon Feb 07, 2005 11:48 am
Post subject: Re: efficiency using or within an sql statement on mysql [Login to view extended thread Info.]
Archived from groups: mailing>database>mysql (more info?)

Fred wrote:
 > The performance of any "or" statement is really really bad.
  > ...where (
  > (addresses.telephone_1 = '+44 (0) 122 464 264 7') or
  > (addresses.telephone_2 = '+44 (0) 122 464 264 7') or
  > (addresses.telephone_3 = '+44 (0) 122 464 264 7');

Have you tried using the EXPLAIN statement to get an idea of which
indexes are being used in your queries, whether using OR or querying
each field individually?
See <a rel="nofollow" style='text-decoration: none;' href="http://dev.mysql.com/doc/mysql/en/explain.html" target="_blank">http://dev.mysql.com/doc/mysql/en/explain.html</a>

Do you have indexes on all three telephone fields? It seems like you
should.

Have you read the chapter of the MySQL doc on performance optimization?
It is worthwhile reading for anyone using MySQL.
<a rel="nofollow" style='text-decoration: none;' href="http://dev.mysql.com/doc/mysql/en/mysql-optimization.html" target="_blank">http://dev.mysql.com/doc/mysql/en/mysql-optimization.html</a>

Regards,
Bill K.

 >> Stay informed about: efficiency using or within an sql statement on mysql 
Back to top
Login to vote
Fred5

External


Since: Jun 10, 2004
Posts: 4



(Msg. 2) Posted: Tue Feb 08, 2005 2:08 am
Post subject: Re: efficiency using or within an sql statement on mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks for your time Bill,

I will look at both those sugestions.
regards,
Fred

 >> Stay informed about: efficiency using or within an sql statement on mysql 
Back to top
Login to vote
Fred5

External


Since: Jun 10, 2004
Posts: 4



(Msg. 3) Posted: Tue Feb 08, 2005 6:38 am
Post subject: Re: efficiency using or within an sql statement on mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks for your time Bill,

I will look at both those sugestions.
regards,
Fred
 >> Stay informed about: efficiency using or within an sql statement on mysql 
Back to top
Login to vote
Fred5

External


Since: Jun 10, 2004
Posts: 4



(Msg. 4) Posted: Wed Feb 09, 2005 11:05 am
Post subject: Re: efficiency using or within an sql statement on mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

ok,

I have spent a few more hours here are my result;


this is the basic query (method 1) I want to execute

---------- start -----------
select customers.account_nb, customers.name, addresses.telephone_1,
addresses.telephone_2, addresses.telephone_3, addresses.post_code
from customers, addresses
where (
(addresses.telephone_1 = '+44 (0) 122 464 264 7') or
(addresses.telephone_2 = '+44 (0) 122 464 264 7') or
(addresses.telephone_3 = '+44 (0) 122 464 264 7') or
( (addresses.address_1 like '11 West Street%') and
(addresses.post_code = 'EX13 5NU') ) or

( (addresses.address_1 like '11 West Street%') and (customers.name
like 'Aberdeen') )
)
and addresses.id = customers.address_id ;
----------- end ----------

which I also split in 5 statement one coresponding to each or
statement
(method 2) as folow



-------------- start ----------
select customers.account_nb, customers.name, addresses.telephone_1,
addresses.telephone_2, addresses.telephone_3, addresses.post_code from
customers, addresses where (addresses.telephone_1 = '+44 (0) 122 464
264 7') and addresses.id = customers.address_id ;

select customers.account_nb, customers.name, addresses.telephone_1,
addresses.telephone_2, addresses.telephone_3, addresses.post_code from
customers, addresses where (addresses.telephone_2 = '+44 (0) 122 464
264 7') and addresses.id = customers.address_id ;

select customers.account_nb, customers.name, addresses.telephone_1,
addresses.telephone_2, addresses.telephone_3, addresses.post_code from
customers, addresses where (addresses.telephone_3 = '+44 (0) 122 464
264 7') and addresses.id = customers.address_id ;

select customers.account_nb, customers.name, addresses.telephone_1,
addresses.telephone_2, addresses.telephone_3, addresses.post_code from
customers, addresses where ( (addresses.address_1 like '11 West
Street%') and (addresses.post_code = 'EX13 5NU') ) and addresses.id =
customers.address_id ;

select customers.account_nb, customers.name, addresses.telephone_1,
addresses.telephone_2, addresses.telephone_3, addresses.post_code from
customers, addresses where ( (addresses.address_1 like '11 West
Street%') and (customers.name like 'Aberdeen') ) and addresses.id =
customers.address_id ;

---------------end ------------

method 1 took 3.00s
method 2 took 9.74s


Then I created indexes
first on telephone_1

method 1 took 2.73s
method 2 took 7.40s


Then I created an indexe
on telephone_2

method 1 took 2.73s
method 2 took 5.00s

Then I created an indexe
on telephone_3

method 1 took 2.73s
method 2 took 3.03s

Then I created an indexe
on address_1

method 1 took 2.73s
method 2 took 0.75s


In this case as this statement is one of the most used by users
I have once again choseen to use the 5 statements split and
converge them programaticaly...

I still don't get why the use of or is much slower (even with
every search field indexed )than the sum of the diferent select
making it.






telephone_1, 2, and 3
address_1,
post_code
name
 >> Stay informed about: efficiency using or within an sql statement on mysql 
Back to top
Login to vote
Bill Karwin1

External


Since: Jun 17, 2004
Posts: 42



(Msg. 5) Posted: Thu Feb 10, 2005 4:39 pm
Post subject: Re: efficiency using or within an sql statement on mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Fred wrote:
 > I still don't get why the use of or is much slower (even with
 > every search field indexed )than the sum of the diferent select
 > making it.

I suggest you use the EXPLAIN statement to see what indexes are being
exercised in both types of queries. MySQL can disregard some of the
indexes if it believes they won't help.

Regards,
Bill K.
 >> Stay informed about: efficiency using or within an sql statement on mysql 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Tuning Mysql 4.1.10 - I have a freebsd box with 1 gig of memory for mysql/php, I was wondering what my my.cnf should look like, I am doing alot of searches and writes on ISAM tables, I currently have the following: [client] port=3306 socket=/tmp/mysql.sock [mysqld]..

Mysql InnoDB PHP - i have a strange problem : MySQL 4.0.21 innodb and php 4.3.9-1 all tables are in innodb type. For a simple select statement, sometimes i get result and sometimes nothing (i.e mysql_num_rows()>0 or mysql_num_rows()==0). It's a random behaviour. If my....

Installation and config of MYSQL - Hi, Need help. Installed Mysql in Redhat 9.0 workstation. I have installed MYSQL,Appache,PHP. PHP,Appache installation is fine and intergration is also working fine. I have installed and configured MYSQL Problem : I cannot log into MYSQL database either...

MySQL, MyODBC and ACCESS 2003 - Ok I have an interesting problem. I have written a database in Access to connect to our web shop and update prices etc from a supplier feed. It works almost perfectly! The prices are doubles, I am using ADO in access to connect via myodbc (with options....

com.mysql.jdbc.MysqlDataTruncation: - I just upgraded to MySQL to 4.1.9 and JDBC mysql-connector-java-3.1.6-bin.jar and now getting the following Exception: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data truncated for column 'usaCard' at row 1 What causes this exception? I've...
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]