473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How does a MySQL "AND" work? (sub-searches)?

Hello,
When I do a "AND" in a SQL query, eg. SELECT NAME WHERE SEX="male" AND
AGE= "30", MySQL does a sub-search, right? i.e. it does the "WHERE
SEX=male" first, and then searches through the list of *those* results
to see if AGE=30?
Does it do this from left to right? i.e. in the query above, would the
table always, and necessarily, get searched for "SEX=male" first, and
"AGE=30" second?
Thanks very much.

Nov 4 '05 #1
7 1740
bi**********@hotmail.com wrote:
Hello,
When I do a "AND" in a SQL query, eg. SELECT NAME WHERE SEX="male" AND
AGE= "30", MySQL does a sub-search, right? i.e. it does the "WHERE
SEX=male" first, and then searches through the list of *those* results
to see if AGE=30?
Does it do this from left to right? i.e. in the query above, would the
table always, and necessarily, get searched for "SEX=male" first, and
"AGE=30" second?


If 'age' had an index on it and 'sex' didn't then it would most likely
search 'age' first. If they both had indexes and one of them had more
different values than the other (eg 'age' in your example) then that
one would probably be seached on first. If neither had indexes then I
would assume it would be in the order you specify, but at the end of
the day if you don't index either column does it really matter what the
DBMS does?

If you're wanting to find out what sort of stuff is being searched on
when doing a query, do "explain ..." (eg explain select foo from bar
where ...)

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Nov 4 '05 #2
Uhh yeah, it does matter, for what I'm trying to implement, otherwise I
wouldn't have asked!.... :)

"Probably" doesn't do me any good in both cases - I need to know -how
MySQL works- internally.

Posted to mysql-internals after reading this </self-thwap!>

Thanks for your reply tho. :)

Nov 4 '05 #3
bi**********@hotmail.com wrote:
: Uhh yeah, it does matter, for what I'm trying to implement, otherwise I
: wouldn't have asked!.... :)

Do you have an example where it would matter?

Nov 4 '05 #4

Malcolm Dew-Jones wrote:
bi**********@hotmail.com wrote:
: Uhh yeah, it does matter, for what I'm trying to implement, otherwise I
: wouldn't have asked!.... :)

Do you have an example where it would matter?


If I'm searching for say, "Males" who are "30" who wear "Boxers", or
whose favourite sport is "Tennis", it would be very reassuring to know
that by the time it gets to "Sport", all "Women" have been left out of
the search, ie. it's not going through the whole table at that point,
otherwise it would be better to use some other method altogether (not a
SQL db at all).

Nov 5 '05 #5
>> : Uhh yeah, it does matter, for what I'm trying to implement, otherwise I
: wouldn't have asked!.... :)

Do you have an example where it would matter?
If I'm searching for say, "Males" who are "30" who wear "Boxers", or
whose favourite sport is "Tennis", it would be very reassuring to know
that by the time it gets to "Sport", all "Women" have been left out of
the search, ie. it's not going through the whole table at that point,


If it's got an index on "age", and age = 30 yields fewer records
than the index on sex = 'Male', it makes sense for MySQL to start
looking at records with age = 30 using the index and then check the
other criteria as it fetches the records.

If there is no index at all (on a simple query with no joins), it's
going to have to scan all the records once. It will NOT do one
scan for 'Male', then scan those records for '30', then scan those
records for 'Tennis'. It checks each record for all the criteria.

otherwise it would be better to use some other method altogether (not a
SQL db at all).


Gordon L. Burditt
Nov 5 '05 #6
bi**********@hotmail.com wrote:

: Malcolm Dew-Jones wrote:
: > bi**********@hotmail.com wrote:
: > : Uhh yeah, it does matter, for what I'm trying to implement, otherwise I
: > : wouldn't have asked!.... :)
: >
: > Do you have an example where it would matter?

: If I'm searching for say, "Males" who are "30" who wear "Boxers", or
: whose favourite sport is "Tennis", it would be very reassuring to know
: that by the time it gets to "Sport", all "Women" have been left out of
: the search, ie. it's not going through the whole table at that point,
: otherwise it would be better to use some other method altogether (not a
: SQL db at all).

That sounds like a 100% generic usage of a relational database - exactly
the sort of thing mysql is designed to do. I think your concern is
unfounded.

You would be better off learning the various capabilities of mysql rather
than programming your own db software replacement in an attempt to
optimize this query.

--

This programmer available for rent.
Nov 5 '05 #7
bi**********@hotmail.com wrote:
If I'm searching for say, "Males" who are "30" who
wear "Boxers", or
whose favourite sport is "Tennis", it would be very
reassuring to know
that by the time it gets to "Sport", all "Women"
have been left out of
the search, ie. it's not going through the whole table at that point,
otherwise it would be better to use some other method altogether (not a
SQL db at all).


As it was pointed out here couple times, MySQL will use indexes to perform
such search IF they exist. The thing is: you can easily create an index
yourself and thus steer MySQL, so to speak, to search exactly the way you
want it.

You can create indexes for several fields at ones, and that will be the
index that the system will use. Pay attention to the order of fields in
the index. In your example you want to include sport first because there
are less chances someone like a particular sport than to belong to a
particular sex (~50%), and therefore the system will search through less
record. Next include age for the same reason: less records will show up
for a particular age since you have more choices than just two. The last
table to include in this index will be sex. By the time the system will
get to searching for the sex, the record will already be narrowed down so
well by previous two searches (sport,age) that it will not matter if you
have too many matches.
The SQL code for creating such index will look like this:

CREATE INDEX `index_name` ON `table_name` (sport,age,sex);

Like I said, pay attention to the order of the fields as you could easily
increase time to search thru the database by a factor of 10 (or more!) by
picking a wrong order (sex,age,sport) although the end result will be the
same set of record.

Goof luck!

--
Cheers,
Dmitri
See Site Sig Below
-------------------------------------
--
##-----------------------------------------------##
Article posted with Web Developer's USENET Archive
http://www.1-script.com/forums
Web and RSS gateway to your favorite newsgroup -
mailing.database.mysql - 1596 messages and counting!
##-----------------------------------------------##
Nov 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dan Anderson | last post by:
I ran a search through the mySQL manual and google and could not find a satisfactory answer. Does mySQL support the declaration of a boolean data type? Currently I am using VARCHAR(6)s with...
0
by: laurie | last post by:
Hi all, Does MySQL support inheritance? i.e. I have Teacher, Student, etc. and they all inherit from Person because they share common attributes (name, address, etc). I believe MySQL is not Object...
2
by: David Nettles | last post by:
Two questions: (1) Does MySQL support VIEWS? (2) Does MySQL support STORED PROCEDURES? -- David Nettles web: http://www.miteyo.org email: tetsuoni3000@yahoo.co.jp
0
by: Christopher Koh | last post by:
Does MYSQL support transactions like rollback and commit now? How about stored procedures and subqueries?
2
by: c-bass | last post by:
How many database does MySQL Support?
1
by: Matias Silva | last post by:
Does MySQL support GUID and UUID column types? Thanks, Matt
1
by: pratapkonakala | last post by:
Does Mysql Server have JVM in it.I want to know can we write java stored procedures in Mysql server itself.If possible please provide me the solution.
2
by: shreedhan | last post by:
Hi I am just learning mysql and php I use Fedora 6 for that One thing I would like to ask is where does mysql store all those information? like tables, databases, all? I would like to save them...
1
by: varathasiva | last post by:
Dear All, I am new in MySQL.I installed mysql-5.0.51b-win32 version.How to configure in MySQL server work as local network or remote server.PostgreSQL we did configuration in pghba.conf host ...
2
bagrigsb
by: bagrigsb | last post by:
Recently I downloaded the latest version of MySQL for 32-bit, installed it. No errors installing it. When I go to run it though, all it does is flash a DOS-like window for a fraction of a second...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.