高性能的MySQL(5)索引策略一压缩,冗余,重复,索引和锁
一、压缩索引MyISAM使用前缀压缩来减少索引的大小,默认只压缩字符串,但是通过设置也可以对整数做压缩。压缩可以使用更少的空间,代价是某些操作可能更慢。特别
一、压缩索引
MyISAM使用前缀压缩来减少索引的大小,默认只压缩字符串,但是通过设置也可以对整数做压缩。
压缩可以使用更少的空间,代价是某些操作可能更慢。特别是倒序的查询,测试表明,对于CPU密集型的应用,查询会很慢,特别是倒序。对于I/O密集型应用,查询可能会不错。
可以在CREATE TABLE的语句中指定PACK_KEYS参数来指定索引压缩方式。
二、冗余和重复索引
重复索引:指在相同的列上按照相同的顺序创建的相同类型的索引,要尽量避免重复索引,除非在同一列上创建不同类型的索引来满足不同的查询需求。比方说 key(col) 和 fulltext key(col)。
冗余索引:如果创建了索引(A,B),再创建索引(A)就是冗余索引。因为索引(A,B)也可以当作(A)来使用(只针对B-Tree)。但是如果再创建索引(B,A)或者(B),则不是冗余索引。或者类型不同,比方说哈希,全文索引等。
冗余索引通常发生在添加索引的时候,大多数情况下都不需要冗余索引,而是尽量扩展已有的索引,除非扩展已有的索引会导致索引很大,,从而影响其他使用索引的性能。
例如:在一个整数列的索引上扩展一个很长的varchar列的索引,性能可能就会急剧下降。但是增加一个新的索引,就会对增删改操作影响很大,所以要平衡使用。
如何找到这写索引,以便删除,可以访问INFORMATION_SCHEMA,或者一些现有的工具来定位。
有一个值得注意的地方:
对于InnoDB因为二级索引包换了主键,所以列(A)上的索引就相当与(A,ID),所以类似
where A=5 order by id 这样的查询,这个索引会很有用。但是扩展为索引(A,B)
之后,则实际变成了(A,B,ID),则order by就无法用到索引排序了。
三、索引和锁
索引可以让查询锁定更少的行,因为索引可以让查询不访问那些不需要的行,那么就会锁定更少的行。这有2点好处:
1、减少锁定行带来的额外的开销。
2、锁定超过需要的行会增加锁争用和减少并发性。
InnoDB只有在访问行的时候才会对其加锁,而索引能减少InnoDB访问的行数,从而减少锁的数量。但这只有当InnoDB在存储引擎层能够过滤掉所有不需要的行时才有效。如果索引无法过滤掉无效的行,那么在InnoDB检索到数据返回给服务器层以后,MySQL服务器才能应用WHERE子句进行过滤。这时候已经无法避免锁定行了,InnoDB已经锁定了所有返回的数据行。
举例说明:表结构
CREATE TABLE `emp3` ( `id` int(11) NOT NULL DEFAULT '0', `name` varchar(100) NOT NULL, `job` varchar(100) NOT NULL, `num1` int(10) DEFAULT NULL, `num2` int(10) DEFAULT NULL, `num3` int(10) DEFAULT NULL, `job_num` int(10) DEFAULT NULL, `d` date DEFAULT NULL, PRIMARY KEY (`id`), KEY `job_num` (`job_num`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;数据如下:
在一个终端执行如下的操作:
虽然只返回了10002-10004之间的行,但是实际上10001-10004都被锁定了
也就是说,底层存储引擎的操作是从索引开头开始获取满足id
我们来证明第一行确实是被锁定了,保持这个终端链接不关闭,然后我们打开另一个终端。
这个查询会挂起,知道第一个事务释放了第一行的锁。
按照这个例子,即使使用了索引,InnoDB也可能锁住一些不需要的数据。如果不能使用索引查找和锁定行的话,结果会更糟。MySQL会全表扫描并锁住所有的行,而不管是不是需要。
本文出自 “phper-每天一点点~” 博客,请务必保留此出处

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.
