>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 4 2032
<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.
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
"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.
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? :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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.
|
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...
|
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
|
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...
| |
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
|
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
|
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...
|
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
|
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...
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |