search
HomeDatabaseMysql TutorialMonty说MySQL的优化(五)_MySQL

 二十一、MySQL表高速缓存工作原理

  每个MyISAM表的打开实例(instance)使用一个索引文件和一个数据文件。如果表被两个线程使用或在同一条查询中使用两次,MyIASM将共享索引文件而是打开数据文件的另一个实例。

  如果所有在高速缓存中的表都在使用,缓存将临时增加到比表缓存尺寸大些。如果是这样,下一个被释放的表将被关闭。

  你可以通过检查mysqld的Opened_tables变量以检查表缓存是否太小。如果该值太高,你应该增大表高速缓存。

  二十二、MySQL扩展/优化-提供更快的速度

  使用优化的表类型(HEAP、MyIASM或BDB表)。

  对数据使用优化的列。

  如果可能使用定长行。

  使用不同的锁定类型(SELECT HIGH_PRIORITY,INSERT LOW_PRIORITY)

  Auto_increment

  REPLACE (REPLACE INTO table_name VALUES (...))

  INSERT DELAYED

  LOAD DATA INFILE / LOAD_FILE()

  使用多行INSERT一次插入多行。

  SELECT INTO OUTFILE

  LEFT JOIN, STRAIGHT JOIN

  LEFT JOIN ,结合IS NULL

  ORDER BY可在某些情况下使用键码。

  如果只查询在一个索引中的列,将只使用索引树解决查询。

  联结一般比子查询快(对大多数SQL服务器亦如此)。

  LIMIT

  SELECT * from table1 WHERE a > 10 LIMIT 10,20

  DELETE * from table1 WHERE a > 10 LIMIT 10

  foo IN (常数列表) 高度优化。

  GET_LOCK()/RELEASE_LOCK()

  LOCK TABLES

  INSERT和SELECT可同时运行。

  UDF函数可装载进一个正在运行的服务器。

  压缩只读表。

  CREATE TEMPORARY TABLE

  CREATE TABLE .. SELECT

  带RAID选项的MyIASM表将文件分割成很多文件以突破某些文件系统的2G限制。

  Delay_keys

  复制功能

  二十二、MySQL何时使用索引

  对一个键码使用>, >=, =, 1 and key_part1

  如果使用HEAP表且不用=搜索所有键码部分。

  在HEAP表上使用ORDER BY。

  如果不是用键码第一部分

  SELECT * FROM table_name WHERE key_part2=1

  如果使用以一个通配符开始的LIKE

  SELECT * FROM table_name WHERE key_part1 LIKE '%jani%'

  搜索一个索引而在另一个索引上做ORDER BY

  SELECT * from table_name WHERE key_part1 = # ORDER BY key2

  二十四、学会使用EXPLAIN

  对于每一条你认为太慢的查询使用EXPLAIN!

  mysql> explain select t3.DateOfAction, t1.TransactionID

  -> from t1 join t2 join t3

  -> where t2.ID = t1.TransactionID and t3.ID = t2.GroupID

  -> order by t3.DateOfAction, t1.TransactionID;

  +-------+--------+---------------+---------+---------+------------------+------+---------------------------------+

  | table | type | possible_keys | key | key_len | ref | rows | Extra |

  +-------+--------+---------------+---------+---------+------------------+------+---------------------------------+

  | t1 | ALL | NULL | NULL | NULL | NULL | 11 | Using temporary; Using filesort |

  | t2 | ref | ID | ID | 4 | t1.TransactionID | 13 | |

  | t3 | eq_ref | PRIMARY | PRIMARY | 4 | t2.GroupID | 1 | |

  +-------+--------+---------------+---------+---------+------------------+------+---------------------------------+

  ALL和范围类型提示一个潜在的问题。

  二十五、学会使用SHOW PROCESSLIST

  使用SHOW processlist来发现正在做什么:

  +----+-------+-----------+----+---------+------+--------------+-------------------------------------+

  | Id | User | Host | db | Command | Time | State | Info |

  +----+-------+-----------+----+---------+------+--------------+-------------------------------------+

  | 6 | monty | localhost | bp | Query | 15 | Sending data | select * from station,station as s1 |

  | 8 | monty | localhost | | Query | 0 | | show processlist |

  +----+-------+-----------+----+---------+------+--------------+-------------------------------------+

  在mysql或mysqladmin中用KILL来杀死溜掉的线程

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function