473,854 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

newbe Q: how to optimize this query?

>From the following MySQL command:

EXPLAIN SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id) JOIN t3 ON t3.name
= t1.name WHERE t1.id IN(123, 124);

which result is:
-------------------------------------------------------------------------------------
table type possible_key key key_len ref rows Extra
t1 const PK, name PK 4 const 10
t3 const PK PK 4 const 10
t2 ref PK PK 4 const 500
Using Where
-------------------------------------------------------------------------------------
where PK is PRIMARY, how can I optimize this query??

It looks to me that for table t1, there are two possible keys, t1.id
and t1.name, since both of them are used in the query, so both of them
should be indexed to optimize the query.. But MySQL uses only one index
per-query and per-table, so I have to use a composite index on table
"t1"?? Is this correct???
From the resulting EXPLAIN list, I thought I used only indexed PK on

the query, the PK is possibly t1.id or the composite of (id,name)???
how can I know if I had used indexes of both columns(id, name)??

Is there any ways that I should take to optimize this query??

Thank you very much for your input and hints.

Huaer

Mar 8 '06 #1
4 2037
<Hu******@gmail .com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .
It looks to me that for table t1, there are two possible keys, t1.id
and t1.name, since both of them are used in the query, so both of them
should be indexed to optimize the query.. But MySQL uses only one index
per-query and per-table, so I have to use a composite index on table
"t1"?? Is this correct???


The MySQL optimizer tries to make the best choices. Even if you had another
index (and suppose MySQL could employ more than one index per table), it
might choose not to use it, if it estimated that using it would cost more
than it would benefit.

For instance, if the PK index is used to reduce the rows to a small number,
then it's very inexpensive to do the remaining JOIN with a full table-scan.
In such cases, it might actually be cost more than it saves to load another
index into memory. The MySQL optimizer tries to notice things like that and
may choose not to use an index even if one is available.

Here's an analogy: if you drive your car 20 miles to a gas station that has
cheaper prices, but you burn more gas than you save by doing so. Or you do
this when your tank is nearly full anyway, so the amount of cost benefit is
very little.

Regards,
Bill K.
Mar 8 '06 #2
Bill Karwin wrote:
<Hu******@gmail .com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .
It looks to me that for table t1, there are two possible keys, t1.id
and t1.name, since both of them are used in the query, so both of them
should be indexed to optimize the query.. But MySQL uses only one index
per-query and per-table, so I have to use a composite index on table
"t1"?? Is this correct???


The MySQL optimizer tries to make the best choices. Even if you had another
index (and suppose MySQL could employ more than one index per table), it
might choose not to use it, if it estimated that using it would cost more
than it would benefit.

For instance, if the PK index is used to reduce the rows to a small number,
then it's very inexpensive to do the remaining JOIN with a full table-scan.
In such cases, it might actually be cost more than it saves to load another
index into memory. The MySQL optimizer tries to notice things like that and
may choose not to use an index even if one is available.

Here's an analogy: if you drive your car 20 miles to a gas station that has
cheaper prices, but you burn more gas than you save by doing so. Or you do
this when your tank is nearly full anyway, so the amount of cost benefit is
very little.


Hi, Bill:
Thank you very much for your info. :-) but this is actually an
interview question, so I should have to find a way to optimize it
instead of letting MySQL do it..:-(.. would you please give me some
more hints especially about using EXPLAIN or composite indexes?? I had
read the documentation about optimizating SELECT on MySQL AB's website,
but still cannot make sure about the solutions..

Many thanks..
Huaer

Mar 8 '06 #3
"huaer" <Hu******@gmail .com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.com.. .
Thank you very much for your info. :-) but this is actually an
interview question, so I should have to find a way to optimize it
instead of letting MySQL do it..:-(.. would you please give me some
more hints especially about using EXPLAIN or composite indexes?? I had
read the documentation about optimizating SELECT on MySQL AB's website,
but still cannot make sure about the solutions..


If I do, do I get the job?

Bill K.
Mar 8 '06 #4
huaer wrote:
Bill Karwin wrote:
<Hu******@gma il.com> wrote in message
news:11****** *************** *@v46g2000cwv.g ooglegroups.com ...
It looks to me that for table t1, there are two possible keys, t1.id
and t1.name, since both of them are used in the query, so both of them
should be indexed to optimize the query.. But MySQL uses only one index
per-query and per-table, so I have to use a composite index on table
"t1"?? Is this correct???


The MySQL optimizer tries to make the best choices. Even if you had another
index (and suppose MySQL could employ more than one index per table), it
might choose not to use it, if it estimated that using it would cost more
than it would benefit.

For instance, if the PK index is used to reduce the rows to a small number,
then it's very inexpensive to do the remaining JOIN with a full table-scan.
In such cases, it might actually be cost more than it saves to load another
index into memory. The MySQL optimizer tries to notice things like that and
may choose not to use an index even if one is available.

Here's an analogy: if you drive your car 20 miles to a gas station that has
cheaper prices, but you burn more gas than you save by doing so. Or you do
this when your tank is nearly full anyway, so the amount of cost benefit is
very little.

Hi, Bill:
Thank you very much for your info. :-) but this is actually an
interview question, so I should have to find a way to optimize it
instead of letting MySQL do it..:-(.. would you please give me some
more hints especially about using EXPLAIN or composite indexes?? I had
read the documentation about optimizating SELECT on MySQL AB's website,
but still cannot make sure about the solutions..

Many thanks..
Huaer

then are you really the right candidate for the job? :)
Mar 8 '06 #5

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

Similar topics

0
1801
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for "optimize table", but (seemingly) the server version 4.0.16 requires INSERT as well. Is the INSERT privelege necessary for performing optimize in mysql
6
2134
by: Bruce D | last post by:
Could someone please help to explain why the following query isn't using the index... explain select id from kbm where state = 'MA' table type possible_keys key key_len ref rows Extra kbm ALL State NULL NULL NULL 1000000 Using where The field 'State' is Char(2). It has an index.
5
1789
by: xeqister | last post by:
Greetings all, We have a complicated statement in DB2 which takes long hour to complete and we have created most of the indexes. Does anybody knows how to tune the following statement to optimize the query? "SELECT AL3.LAB, AL3.DEPARTMENT, AL6.NAME, Count (AL4.ITEM), AL5.SALES_TERRIOTARY, AL1.DATE_SERVICE FROM YR2005.REQUESTX AL1, YR2005.RESULTP AL2, YR2005.PANEL AL3, YR2005.RESULTV AL4, YR2005.DOCTOR AL5, YR2005.DEPT AL6 WHERE...
3
2829
by: Reddy | last post by:
The sql query for my datagrid returns 100, 000 records. But the datagrid should display 20 records per page. I am using datagrid paging, but it is taking too much time for the page to load. Is there any way I can optimize the speed. Any sample code would be great. Thanks, Reddy
13
3131
by: Frank Swarbrick | last post by:
IBM has a product for the VSE operating system called the VSAM Redirector. It allows you to use VSAM to access RDBMS tables/views as if they were actual VSAM files. We're doing a comparison right now between Oracle, which we've been running in production for many years, and DB2, which we are just starting with. One of the tests I am trying is to see how efficient the VSAM Redirector works with DB2 versus Oracle. Below are two types of...
17
2733
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not pattern1 print >>orcfilename, line I am pretty sure my code isn't close to what I want. I need to be able
5
1932
by: troy | last post by:
Could someone please point me in the right direction on how to read in a large query with .net. I am trying to emulate a legacy database system so I don't know the upper bounds of the sql query. An example query would be something like: Select * from invoices where year 1995 the query must be updatable and only return say 10 to 100 rows at a
1
1780
by: acornejo | last post by:
Hi All I've the following code I need to optimize. Currently tblOutgoing is about 250K registers and growing at a rate of about 20k records per day. This code takes me over 5 secs to run on each iteration. How can I optimize the mysql query in order to make it faster? table tblOutgoing has an unique id slog_id. if not, how can I add indexes to this table? this table is constantly queried (selects/updates/inserts) based on several field...
3
2506
zabsmarty
by: zabsmarty | last post by:
Can any one help me to make my query code optimize and load faster. Please help me or any example what steps should we use to optimize. Thank You
0
9899
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
9750
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,...
1
10740
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
10362
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...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5738
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
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3182
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.