Home Database Mysql Tutorial mysql性能tip(1)_MySQL

mysql性能tip(1)_MySQL

Jun 01, 2016 pm 01:44 PM
string use

bitsCN.com

数据类型选择方面的几个原则:

 

1,更小通常更好,选择能正确表示数据的最小类型。

 

2,简单就好,用简单类型优于用复杂类型。

 

3,避免NULL,尽量定义字段为not null。性能提升很小。

 

 

 

字符串的选型:

 

当最大长度远远大于平均长度时,并很少发生更新时,选择VARCHAR。

 

CHAR适合保存用户密码的MD5哈希值。

 

MySql存储的最小时间单位是秒,但是可以用毫秒进行临时计算。

 

 

 

InnoDB有一个特别功能叫做自适应哈希索引,当InnoDB注意到一些索引值被很频繁的访问的时候,它就会在BTree的顶端为这些值建立内存索引。

 

 

 

索引包含了来自于表中的一列或者多列的值,如果索引了多列数据,列的顺序非常重要,因为Mysql只能高效的搜索所以你的最左前缀。

 

 

 

聚集索引:不是一种单独的索引类型,而是一种存储数据的方式。当表有聚集索引的时候,它的数据行实际保存在索引的叶子页中。每个表只能有一个聚集索引。

 

使用时注意设置前缀索引。索引选择要注意不重复的索引值。

 

 

 

索引策略总结:

 

检查最常用的查询,不要不调查就随便建索引。为了找到建索引的平衡,应该测试和剖析。第一检查的就是响应时间,要为耗时很长的查询添加索引;然后就是检查导致最大负载的查询,添加索引;最好要把系统的cpu、内存和磁盘瓶颈考虑进去。

 

在任何可能的地方,都要试着扩展索引,而不是新增索引。通常维护一个多列索引要比维护多个单列索引容易。如果不知道查询的分布,就要尽可能的使索引变得更有选择性,因为高选择性的索引通常更有好处。

 

 

 

在冗余方面:

 

通常需要额外的索引、冗余的字段,或者甚至缓存表和汇总表来加速读取。这为写入查询和维护工作增加了工作量,但这仍然是设计高性能查询的常见技巧:用极大的加速读取来弥补较慢的写入的代价。(但是也为读写增加了开发复杂性)

 

 

 

InnoDB存储引擎的一些tip:

 

 

事务性:支持事务和四种事务隔离级别。

 

外键:在Mysql 5.0中唯一的支持外键的存储引擎。

 

行级锁:锁设定于行一级,不会向上传递并且也不会阻塞选择——标准选择根本不会设定任何锁,有很好的并发特性。

 

多版本:InnoDB使用多版本并发控制,默认情况下不会选择读取陈旧数据。

 

按主键聚集:所有InnoDB表都是按主键聚集的。

 

所有索引包含主键列:索引按照主键引用行,因此,如果不把主键维持的很短,索引就会很大。

 

优化的缓存:InnoDB把数据和内存缓存在缓冲区池里。会自动构建哈希索引来加快行读取。

 

未压缩的索引:索引没有使用前缀压缩,因此可能会比MyISAM表的索引大很多。

 

数据装载缓慢:在MySql5.0中,InnoDB不会特别优化数据加载。它一次构建一行的索引,而不是按照排序进行构建。导致加载缓慢。

 

阻塞AUTO_INCREMENT:在MySql5.1之前的版本,InnoDB使用了表级锁来产生每个新的auto_increment值。

 

没有缓存的count(*)值:和MyISAM表或Memory表不同,InnoDB表不会把表的行数保存在表中,这意味着没有where子句的count(*)查询不会被优化掉,并且需要全表或索引扫描。

 

 

查询优化:

 

在下面的应用场景中,在应用程序端进行联接效率更高:可以缓存早期查询的大量数据;使用了多个MyISAM表;数据分布在不同的服务器上;对于大表使用in()替换联接;一个联接引用了同一个表很多次。

 

 

 

查询缓存:Mysql对查询缓存的实现就是将查询文本作为key,将结果集作为value的一个map。因此,查询语句的任何一点变化都会导致缓存不命中,书写一致的查询语句就成了mysql操作的要点。另外就是查询缓存不缓存任何包含不确定函数的查询,比如NOW()。换句话说,用户自定义的函数,变量等等都不会被缓存。(原理是这样的,服务器会执行一次不区分大小写的检查来验证查询是否以SEL打头)。另外值得说明的是,结果太大也不会被缓存的。建立缓存也是有开销的,比如内存分配等,开启了缓存后查询就会检查,这也是性能开销,因此开启与否要根据情况仔细斟酌

 

摘自 Change Dir

bitsCN.com
Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

PHP techniques for deleting the last two characters of a string PHP techniques for deleting the last two characters of a string Mar 23, 2024 pm 12:18 PM

As a scripting language widely used to develop web applications, PHP has very powerful string processing functions. In daily development, we often encounter operations that require deleting a string, especially the last two characters of the string. This article will introduce two PHP techniques for deleting the last two characters of a string and provide specific code examples. Tip 1: Use the substr function The substr function in PHP is used to return a part of a string. We can easily remove characters by specifying the string and starting position

See all articles