Académique Documents
Professionnel Documents
Culture Documents
createtableapples(
varietyvarchar(10)primarykey,
notevarchar(50),
priceint,
key(price)
)engine=InnoDB
insertintoapplesvalues
('gala','hello',5),
('fuji','hello',6),
('limbertwig','hello',8),
('reddelicious','hello',3),
('pippin','hello',8),
('grannysmith','hello',11),
('roma','hello',6)
selectvarietyfromappleswhereprice=5
Begin with a table and the data it needs, but without any
indexes except those designed to constrain the data to
s earc h t his webs it e
https://www.xaprb.com/blog/2006/07/04/howtoexploitmysqlindexoptimizations/ 4/11
3/4/2017 HowtoexploitMySQLindexoptimizationsBaronSchwartz'sBlog
Before you start, consider the size of the table and how
much it is used. You should put your optimization effort
where it is most needed. If one 5-minute query runs once
a day and you know it should be possible to optimize it to
5 seconds, thats 4 minutes and 55 seconds saved. If
another query issued every minute takes 5 seconds and
you know it should be possible to run it in a few
milliseconds, thats about 7,000 seconds saved. You should
optimize the second query rst. You should also consider
carefully-designed archiving jobs to get those tables as
small as possible. Smaller tables are a huge optimization.
selectvarietyfromappleswhereprice=?
selectnotefromappleswhereprice=?
createtablesomething(
idbigintnotnullauto_incrementprimaryke
y,
is_somethingtinyintnotnull,
othercol_1bigintnotnull,
othercol_2bigintnotnull,
othercol_3bigintnotnull,
index(is_something)
)
createtabletest(aint,bint,cint)engine=In
noDB
insertintotestvalues(1,1,1),(2,2,2)
connection1:
settransactionisolationlevelserializable
starttransaction
select*fromtest
connection2:
settransactionisolationlevelserializable
starttransaction
updatetestseta=5
TRANSACTION081411,ACTIVE1410sec,process
no8799,OSthreadid1141414240startingindex
read
mysqltablesinuse1,locked1
LOCKWAIT2lockstruct(s),heapsize1216
MySQLthreadid4,queryid194localhostxaprb
Updating
updatetestseta=5
TRXHASBEENWAITING9SECFORTHISLOCK
TOBEGRANTED:
RECORDLOCKSspaceid0pageno131074nbits72
index`GEN_CLUST_INDEX`oftable`test/test`trx
id081411lock_modeXwaiting
Recordlock,heapno2PHYSICALRECORD:n_fields
6compactformatinfobits0
0:len6hex000000018a02asc1:len
6hex000000013e0aasc>2:len7hex
80000000320110asc23:len4hex800
00001asc4:len4hex80000001asc
5:len4hex80000001asc
TRANSACTION081456,ACTIVE17sec,processn
o8799,OSthreadid1141680480startingindexr
ead
mysqltablesinuse1,locked1
LOCKWAIT4lockstruct(s),heapsize1216
MySQLthreadid9,queryid277localhostxaprb
Updating
updatetestseta=5wherea=1
TRXHASBEENWAITING6SECFORTHISLOCK
TOBEGRANTED:
RECORDLOCKSspaceid0pageno131076nbits72
index`PRIMARY`oftable`test/test`trxid081
456lock_modeXlocksrecbutnotgapwaiting
Recordlock,heapno2PHYSICALRECORD:n_fields
5compactformatinfobits0
0:len4hex80000001asc1:len6he
x000000013e27asc>'2:len7hex80000
000320110asc23:len4hex80000001
asc4:len4hex80000001asc
Summary
The more you know about how indexes work, the more
you can optimize your databases. Sometimes these
optimizations dont help much, but sometimes theyre
huge. In this article I explained how InnoDBs primary and
secondary indexes are different from other storage
engines. Now that you understand the differences, you
can understand the optimizations and trade-offs each
storage engine has, and how to take advantage of the
optimizations and avoid the drawbacks if possible. I
showed you several side effects of the index design, such
s earc h t his webs it e
https://www.xaprb.com/blog/2006/07/04/howtoexploitmysqlindexoptimizations/ 10/11
3/4/2017 HowtoexploitMySQLindexoptimizationsBaronSchwartz'sBlog
Newer Older
Comments