473,834 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1766
bi**********@ho tmail.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**********@ho tmail.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**********@ho tmail.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**********@ho tmail.com wrote:

: Malcolm Dew-Jones wrote:
: > bi**********@ho tmail.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**********@ho tmail.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.databas e.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
4954
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 either 'TRUE' or 'FALSE' and would like to cut down on the storage. Thanks in advance, Dan Anderson
0
4237
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 Orientated so it probably wont. If so, how would I implement this in MySQL (I've been doing it with Oracle in the past and want to change over to MySQL)? Any websites out there to show you? Thanks a lot.
2
4546
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
2627
by: Christopher Koh | last post by:
Does MYSQL support transactions like rollback and commit now? How about stored procedures and subqueries?
2
1737
by: c-bass | last post by:
How many database does MySQL Support?
1
5683
by: Matias Silva | last post by:
Does MySQL support GUID and UUID column types? Thanks, Matt
1
1393
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
5724
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 in case of my operating system crashes. I have to create all those tables and make re-entry when I format my computer. So is there anyway to make backup or like that?
1
1954
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 all all 0.0.0.0 0.0.0.0 md5.How we can do settings in MySQL 5.0 server.Please Help me.Advance thanks to all. Kindly Regards varathasiva
2
8799
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 and that's it. Okay, here is what I am doing and the operating system info... I am running XP Home Addition with SP3. In the windows explorer, i am double-clicking on the mysql.exe file when this oocurs.
0
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9643
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10790
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10214
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3079
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.