Tunning sql query
-
Upload
vuhaininh88 -
Category
Technology
-
view
386 -
download
0
Transcript of Tunning sql query
![Page 1: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/1.jpg)
Analyze and tune SQL Query
![Page 2: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/2.jpg)
Why need tune Sql query
Increase performance Save time and money
![Page 3: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/3.jpg)
Measurement Methods
Benchmarking MySQL Enterprise Monitor (MEM), Query Analyzer Performance schema, MySQL sys schema EXPLAIN Optimizer trace Slow log Status variables (SHOW STATUS LIKE ’Handler%’) Profile
![Page 4: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/4.jpg)
Index
Some type of Index:B-Tree indexHash indexR-Tree index
The use of indexes to find rows faster Writes become slower with each added index
![Page 5: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/5.jpg)
Query Processing
![Page 6: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/6.jpg)
EXPLAIN
With EXPLAIN the query is sent all the way to the optimizer, but not to the storage engine
Instead EXPLAIN returns the query execution plan EXPLAIN tells you:
– In which order the tables are read – What types of read operations that are made – Which indexes could have been used – Which indexes are used – How the tables refer to each other – How many rows the optimizer estimates to retrieve from each table
![Page 7: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/7.jpg)
Database Designing (Optimizing Schemas)
![Page 8: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/8.jpg)
Normalization
Normalization is a key factor in optimizing your database structure Good normalization prevents redundant data from being stored in the
same tables By moving redundant data to their own table, this reduces storage
requirements and overhead when processing queries data warehousing and reporting system a star-schema might be a
better solution
![Page 9: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/9.jpg)
Table Optimizations
Use columns that are as short as possible; – INT instead of BIGINT – VARCHAR(10) instead of VARCHAR(255)
Pay special attention to columns that are used in joins Define columns as NOT NULL if possible
![Page 10: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/10.jpg)
Index Optimizations
An index on the whole column is not necessary – Instead index just a prefix of a column – Prefix indexes take less space and the operations are faster
Composite indexes can be used for searches on the first column(s) in the index
Minimize the size of PRIMARY KEYs that are used as references in other tables
Using an auto_increment column can be more optimal A FULLTEXT index is useful for:word searches in text, searches on
several columns
Click to add textClick to add text
![Page 11: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/11.jpg)
Optimizing Where Condition
![Page 12: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/12.jpg)
Selecting Access Method
find the best access method: – Check if the access method is useful – Estimate cost of using access method – Select the cheapest to be used
Choice of access method is cost based Finding the optimal method to read data from storage engine
Main access methods: - Table scan - Index scan - Ref access - Range scan - Index merge - Loose index scan
![Page 13: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/13.jpg)
Ref Access
id select type
table type possible key key length
ref
1 SIMPLE order ref i_o_orderdate i_o_custkey
i_o_orderdate
4
2 SIMPLE customer eq_ref PRIMARY PRIMARY order.c_custkey
EXPLAIN SELECT * FROM orders JOIN customer ON c_custkey = o_custkey WHERE o_orderdate = ‘1992-09-12’
![Page 14: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/14.jpg)
Change Where Condition To Use Index SELECT *
FROM customerWHERE SUBSTRING(c_name,1,1) = 'C'
SELECT *FROM customerWHERE c_name >= 'C' and c_name < ‘D’
SELECT *FROM customerWHERE c_name like 'C%'
![Page 15: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/15.jpg)
Change Where Condition To Use Index Change where condition into
F(field) > const ->filed > G(const)
![Page 16: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/16.jpg)
Force Index - Ignore Index
Select id From data where type = 100 and level > 3order by id;
Select id From data use index(type)where type = 100 and level > 3 order by id;
Select id From data ignore index(type) where type = 100 and level > 3 order by id;
![Page 17: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/17.jpg)
Join Optimizer
![Page 18: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/18.jpg)
Join Optimizer
![Page 19: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/19.jpg)
STRAIGHT_JOIN
STRAIGHT_JOIN is similar to JOIN, except that the left table is always read before the right table. This can be used for those (few) cases for which the join optimizer puts the tables in the wrong order.
![Page 20: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/20.jpg)
Using Filesort
“Filesort”: – Store query result in temporary table before sorting – If data volume is large
Optimizations: – index to generate query result in sorted order – For ”LIMIT n” queries.
![Page 21: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/21.jpg)
Compare Join Vs In
SELECT requestID, request_titleFROM Request rWHERE EXISTS( SELECT * FROM User WHERE userID=r.userID)
SELECT requestID, request_titleFROM Request rJOIN FROM User USING(serID)
![Page 22: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/22.jpg)
Join With Federate Table
CREATE TABLE federated_table (
id INT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(32) NOT NULL DEFAULT '',
other INT(20) NOT NULL DEFAULT '0',
PRIMARY KEY (id),
INDEX name (name),
INDEX other_key (other)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1 CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table';
![Page 23: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/23.jpg)
Question and Answer
![Page 24: Tunning sql query](https://reader031.fdocuments.us/reader031/viewer/2022021507/58f00c2f1a28ab72718b46ad/html5/thumbnails/24.jpg)
Thank you