Mysql入门系列:MYSQL列类型选择与MYSQL查询效率[图]_MySQL
4.3 列类型选择与查询效率
要选择有助于使查询执行更快的列,应遵循如下规则(这里,“BLOB 类型”应该理解为即包含B L O B也包含TEXT 类型):
■ 使用定长列,不使用可变长列。这条准则对被经常修改,从而容易产生碎片的表来说特别重要。例如,应该选择CHAR 列而不选择VARCHAR 列。所要权衡的是使用定长列时,表所占用的空间更多,但如果能够承担这种空间的耗费,使用定长行将比使用可变长的行处理快得多。
■ 在较短的列能够满足要求时不要使用较长的列。如果正使用的是定长的CHAR 列,应该使它们尽量短。如果列中所存储的最长值为40 个字符,那么就不要将其定义为CHAR ( 2 5 5 );只要定义为CHAR(40) 即可。如果能够使用MEDIUMINT 而不是BIGINT,表将会更小(磁盘I/O 也较少),其值在计算中也可以处理得更快。
■ 将列定义为NOT NULL。这样处理更快,所需空间更少。而且有时还能简化查询,因为不需要检查是否存在特例NULL。
■ 考虑使用ENUM 列。如果有一个只含有限数目的特定值的列,那么应该考虑将其转换为ENUM 列。ENUM 列的值可以更快地处理,因为它们在内部是以数值表示的。
■ 使用PROCEDURE ANALYSE( )。如果使用的是MySQL3.23 或更新的版本,应该执行PROCEDURE ANALYSE( ),查看它所提供的关于表中列的信息:
相应输出中有一列是关于表中每列的最佳列类型的建议。第二个例子要求PROCEDURE ANALYSE( ) 不要建议含有多于16 个值或取多于256 字节的ENUM 类型(可根据需要更改这些值)。如果没有这样的限制,输出可能会很长;ENUM 的定义也会很难阅读。根据PROCEDURE ANALYSE( ) 的输出,会发现可以对表进行更改以利用更有效的类型。如果希望更改值类型,使用ALTER TABLE 语句即可。
■ 将数据装入B L O B。用BLOB 存储应用程序中包装或未包装的数据,有可能使原来需要几个检索操作才能完成的数据检索得以在单个检索操作中完成。而且还对存储标准表结构不易表示的数据或随时间变化的数据有帮助。在第3 章ALTER TABLE 语句的介绍中,有一个例子处理存储来自Web 问卷的结果的表。该例子中讨论了在问卷中增加问题时,怎样利用ALTER TABLE 向该表追加列。
解决该问题的另一个方法是让处理Web 的应用程序将数据包装成某种数据结构,然后将其插入单个BLOB 列。这样会增加应用程序对数据进行解码的开销(而且从表中检索出记录后要对其进行编码),但是简化了表的结构,并且不用在更改问卷时对表进行更改。另一方面, BLOB 值也有自己的固有问题,特别是在进行大量的DELETE 或UPDATE 操作时更是如此。删除BLOB 会在表中留下一个大空白,在以后将需用一个记录或可能是不同大小的多个记录来填充。
■ 对容易产生碎片的表使用OPTIMIZE TABLE。大量进行修改的表,特别是那些含有可变长列的表,容易产生碎片。碎片不好,因为它在存储表的磁盘块中产生不使用的空间。随着时间的增长,必须读取更多的块才能取到有效的行,从而降低了性能。任意具有可变长行的表都存在这个问题,但这个问题对BLOB 列更为突出,因为它们尺寸的变化非常大。经常使用OPTIMIZE TABLE 有助于保持性能不下降。
■ 使用合成索引。合成索引列有时很有用。一种技术是根据其他列建立一个散列值,并将其存储在一个独立的列中,然后可通过搜索散列值找到行。这只对精确匹配的查询有效。(散列值对具有诸如“ =”这样的操作符的范围搜索没有用处)。在MySQL3.23版及以上版本中,散列值可利用MD5( ) 函数产生。散列索引对BLOB 列特别有用。有一事要注意,在MySQL3.23.2 以前的版本中,不能索引BLOB 类型。甚至是在3.23.2 或更新的版本中,利用散列值作为标识值来查找BLOB 值也比搜索BLOB 列本身更快。
■ 除非有必要,否则应避免检索较大的BLOB 或TEXT 值。例如,除非肯定WHERE 子句能够将结果恰好限制在所想要的行上,否则SELECT * 查询不是一个好办法。这样做可能会将非常大的BLOB 值无目的地从网络上拖过来。这是存储在另一列中的BLOB 标识信息很有用的另一种情形。可以搜索该列以确定想要的行,然后从限定的行中检索BLOB 值。
■ 将BLOB 值隔离在一个独立的表中。在某些情况下,将BLOB 列从表中移出放入另一个副表可能具有一定的意义,条件是移出BLOB 列后可将表转换为定长行格式。这样会减少主表中的碎片,而且能利用定长行的性能优势。

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

AI Hentai Generator
Generate AI Hentai for free.

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

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

If you want to check the activation date using an Apple mobile phone, the best way is to check it through the serial number in the mobile phone. You can also check it by visiting Apple's official website, connecting it to a computer, and downloading third-party software to check it. How to check the activation date of Apple mobile phone Answer: Serial number query, Apple official website query, computer query, third-party software query 1. The best way for users is to know the serial number of their mobile phone. You can see the serial number by opening Settings, General, About This Machine. . 2. Using the serial number, you can not only know the activation date of your mobile phone, but also check the mobile phone version, mobile phone origin, mobile phone factory date, etc. 3. Users visit Apple's official website to find technical support, find the service and repair column at the bottom of the page, and check the iPhone activation information there. 4. User

MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same

Title: How to use Oracle to query whether a table is locked? In Oracle database, table lock means that when a transaction is performing a write operation on the table, other transactions will be blocked when they want to perform write operations on the table or make structural changes to the table (such as adding columns, deleting rows, etc.). In the actual development process, we often need to query whether the table is locked in order to better troubleshoot and deal with related problems. This article will introduce how to use Oracle statements to query whether a table is locked, and give specific code examples. To check whether the table is locked, we

Forum is one of the most common website forms on the Internet. It provides users with a platform to share information, exchange and discuss. Discuz is a commonly used forum program, and I believe many webmasters are already very familiar with it. During the development and management of the Discuz forum, it is often necessary to query the data in the database for analysis or processing. In this article, we will share some tips for querying the location of the Discuz database and provide specific code examples. First, we need to understand the database structure of Discuz

How to check the latest price of Tongshen Coin? Token is a digital currency that can be used to purchase in-game items, services, and assets. It is decentralized, meaning it is not controlled by governments or financial institutions. Transactions of Tongshen Coin are conducted on the blockchain, which is a distributed ledger that records the information of all Tongshen Coin transactions. To check the latest price of Token, you can use the following steps: Choose a reliable price check website or app. Some commonly used price query websites include: CoinMarketCap: https://coinmarketcap.com/Coindesk: https://www.coindesk.com/ Binance: https://www.bin

Check the latest price of INJ coin. INJ coin is a decentralized finance (DeFi) token based on the Ethereum blockchain developed by InjectiveProtocol and aims to provide an efficient and transparent platform for derivatives transactions. Holders can use INJ coins to pay transaction fees, stake to receive rewards, and participate in the governance of InjectiveProtocol. The total amount of INJ tokens issued is limited, which gives it scarcity and the potential to store value. InjectiveProtocol’s vision is to promote broader financial inclusion through INJ tokens and provide users with an open, borderless financial services experience. The use of INJ tokens not only promotes the innovative development of the DeFi industry, but also provides users with
