web| Page | Optimization example of Web page-turning optimization
Author: Wanghai
Environment:
Linux version 2.4.20-8custom (ROOT@WEB2) (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #3 SMP Thu June 5 22:03:36 CS T 2003
mem:2113466368
swap:4194881536
CPU: Two Hyper-Threading Intel (R) Xeon (TM) CPU 2.40GHz
Optimization before the statement in the MySQL query about 15 seconds out, transfer to Oracle without adjusting the index and statements in the case of the execution time is about 4-5 seconds, adjusted after the execution time is less than 0.5 seconds.
Page Turn statement:
SELECT * FROM (select t1.*, rownum as LineNum from (
SELECT/*+ Index (a ind_old) * *
A.category from Auction_auctions a WHERE a.category = ' 170101 ' and a.closed= ' 0 ' and ends > Sysdate and (a.approve_stat us>=0) Order by A.ends) T1 where RowNum < 18681) where LineNum >= 18641
Sql> Select Segment_name,bytes,blocks from user_segments where segment_name = ' auction_auctions ';
Segment_name BYTES BLOCKS
Auction_auctions 1059061760 129280
The original index on the table
Create INDEX Ind_old on auction_auctions (closed,approve_status,category,ends) tablespace tbsindex compress 2;
Sql> Select Segment_name,bytes,blocks from user_segments where segment_name = ' ind_old ';
Segment_name BYTES BLOCKS
Ind_old 20971520 2560
Tables and indexes have been analyzed, let's take a look at the cost of SQL execution
sql> set Autotrace trace;
Sql> SELECT * FROM (select t1.*, rownum as LineNum from (select A.* to auction_auctions a WHERE a.category like ' 18% ') and a.closed= ' 0 ' and ends > Sysdate and (a.approve_status>=0) Order by A.ends) T1 where RowNum <18681) where Lin Enum >= 18641;
We can see that the SQL statement finds the innermost result set through the index range scan, and then the data is finally drawn through two view operations. of which 18502 consistent gets,17901 physical reads
Let's take a look at whether this index is built in the end, and see the distinct value of each lookup column first.
Select COUNT (distinct ends) from auction_auctions;
COUNT (Distinctends)
-------------------
338965
Sql> Select COUNT (Distinct category) from Auction_auctions;
COUNT (Distinctcategory)
-----------------------
1148
Sql> Select COUNT (Distinct closed) from auction_auctions;
COUNT (distinctclosed)
---------------------
2
Sql> Select COUNT (Distinct approve_status) from auction_auctions;
COUNT (Distinctapprove_status)
-----------------------------
5
Average storage length of columns in a page index
Sql> Select AVG (vsize (ends)) from Auction_auctions;
AVG (Vsize (ENDS))
----------------
7
Sql> Select AVG (vsize (closed)) from Auction_auctions;
AVG (Vsize (CLOSED))
------------------
2
Sql> Select AVG (vsize (category)) from Auction_auctions;
AVG (Vsize (CATEGORY))
--------------------
5.52313106
Sql> Select AVG (vsize (approve_status)) from Auction_auctions;
AVG (Vsize (approve_status))
--------------------------
1.67639401
Let's estimate the size of the various combination indexes and see that the closed,approve_status,category are relatively lower set potential columns (more duplicates), and we'll probably calculate the space needed for the various page indexes below.
Index size=338965*2* (9+2) + 537351* (1.7+5.5+6) =14603998
Index2: (Closed,category,ends,approve_status)
CLOSED:DISTINCT Number---2
CATEGORY:DISTINCT Number---1148
Index size=2*1148* (2+5.5) +537351* (7+1.7+6) =7916279
INDEX3: (closed,approve_status,category,ends)
CLOSED:DISTINCT Number---2
Approve_status:distinct number―5
Index size=2*5* (2+1.7) +537351* (7+5.5+6) =9941030
The results came out, Index2: (closed,category,ends,approve_status) the smallest index
Let's look at the statement again.
SELECT * FROM (select t1.*, rownum as LineNum from (select A.* to auction_auctions a WHERE a.category like ' 18% ' and A.C Losed= ' 0 ' and ends > Sysdate and (a.approve_status>=0) Order by A.ends) T1 where rownum <18681) where linenum ; = 18641;
You can see that this SQL statement has great scope for optimization, first the innermost result set select A.* from Auction_auctions a WHERE a.category like ' 18% ' and a.closed= ' 0 ' and ends > Sysdate and (a.approve_status>=0) Order by A.ends, this will take the index range scan, and then the table scan by ROWID, so if the eligible data is much more resource-intensive, We could rewrite it as
SELECT A.rowid from Auction_auctions a WHERE a.category like ' 18% ' and a.closed= ' 0 ' and ends > Sysdate and (a.approve_s tatus>=0) Order by A.ends
In this case, the innermost result set requires only the index fast full scan to complete, and then rewrite to draw the following statement
SELECT * from Auction_auctions where rowid the Select Rid from (
SELECT T1.rowid RID, rownum as linenum from
(SELECT A.rowid from Auction_auctions a WHERE a.category like ' 18% ' and a.closed= ' 0 ' and ends > Sysdate and
(a.approve_status>=0) ORDER by A.ends) T1 where RowNum < 18681) where LineNum >= 18641)
Now let's test the query cost for this index
SELECT * from Auction_auctions where rowid the Select Rid from (
SELECT T1.rowid RID, rownum as linenum from
(SELECT A.rowid from Auction_auctions a WHERE a.category like ' 18% ' and a.closed= ' 0 ' and ends > Sysdate and
(a.approve_status>=0) ORDER by A.closed,a.ends) T1 where RowNum < 18681) where LineNum >= 18641)
You can see consistent gets from 19437 to 2080,physical reads from 18262 to 1516, and the query time also drops to 0 for 4 seconds. 5 Seconds, you can say the SQL tuning has the desired effect.
And then I changed the statement,
Sql> SELECT * from Auction_auctions where rowid
2 (SELECT rid from (
3 SELECT t1.rowid RID, rownum as linenum from
4 (SELECT A.rowid from Auction_auctions a
5 WHERE a.category like ' 18% ' and a.closed= ' 0 ' and ends > Sysdate and
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.