Mysql optimization learning-find slow query statements

How to add index

1. Add when the table is created

create database table1{

        id primary key not null,

        index|keys index name (id)

}

2. Add in the created table

create index|keys index name on table name (field name)

3. How to use the index Generally, the indexed field is used as the condition of where

Such as: select * from tables where id = 3; (ID is used as an index field)

 

Generally, we can use fields as variables to perform operations, and at the same time, the result of comparison and judgment in sql is 0 and 1 ture is 1 false is 0

For example, select scroe>60 from stu; the result will be 0 and 1

select sum(scroe>60) from stu; so that we can know the number of passers in the class, because every time scroe>60 is executed, it will return 0 or 1, so just sum it up.

4. The difference between where and having   

Where can not be judged by aliases, having can use aliases

For example: select (mark_price-price) as'cj' from marker having cj> 200; if you use where, an error will be reported

 

 

mysql optimization-------------

 

 

1. The rationality of the table design

Don’t write all the fields that can be deduced from the three-paradigm table on the same table

How to check out SQL statements with slow queries

 

 

Slow positioning query

 

Test data generation Use stored procedures to generate with the help of custom functions

mysql definition function

mysql defines stored procedures

 

 

set long_query_time = 3; first set mysql to determine the slow query time to 3 seconds, the default is 10 seconds

In this way, as long as the SQL statement exceeds 3 seconds, it will be recorded in the log, so that the statement can be optimized 

Guess you like

Origin blog.csdn.net/weixin_41421227/article/details/88691362
Recommended