SELECTs -- do's and don'ts

原文地址:http://mysql.rjweb.org/doc.php/ricksrots

Brought to you by Rick James

Here are 160+ tips,tricks,suggestions,etc. They come from a decade of improving performance in MySQL in thousands of situations. There are exceptions to the statements below,but they should help guide you into better understanding how to effectively use MySQL. 

SELECTs -- do's and don'ts

RoTs dt >= '2010-02-01' AND dt < '2010-02-01' + INTERVAL 7 DAY     ⚈  ORDER BY NULL -- a little-known trick to avoid GROUP BY doing a sort (if there is another way).     ⚈  WHERE (a,b) > (7,8) is poorly optimized     ⚈  Gather these to study a slow query: SHOW CREATE TABLE,SHOW TABLE STATUS,EXPLAIN.     ⚈  Do not use OFFSET for pagination -- continue where you "left off"     ⚈  Don't mix DISTINCT and GROUP BY     ⚈  Be explicit about UNION ALL vs UNION DISTINCT -- it makes you think about which to use     ⚈  Do not use SELECT * except for debugging or when fetching into a hash.     ⚈  VIEWs are poorly optimized     ⚈  A subquery in the FROM clause may be useful for retrieving BLOBs without sorting them: Speed up a query by first finding the IDs,then self-JOIN to fetch the rest. ORDER BY id LIMIT 30,10to find the 4th page of 10 items. But it is so inefficient,especially when you have thousands of pages. The thousandth page has to read (at some level) all the pages before it. "Left off" refers to having the "Next" button on one page give the id (or other sequencing info) of where the next page can be found. Then that page simply does WHERE id > $leftoff ORDER BY id LIMIT 10 </tr>

INDEXing

Discussion
RoTs Data_length     ⚈  5 fields in a compound index seems "too many"     ⚈  Having no compound indexes is a clue that you do not understand their power. INDEX(a,b) may be much better than INDEX(a),INDEX(b)     ⚈  INDEX(a,b) covers for INDEX(a),so drop the latter.     ⚈  2x speedup when "Using index" (a "covering" index)     ⚈  Akiban (3rd party) "groups" tables together,interleaved,to improve JOIN performance.     ⚈  FULLTEXT (MyISAM) -- watch out for ft_min_word_len=4,stopwords,and 50% rule     ⚈  A FULLTEXT index will be used before any other index.     ⚈  FULLTEXT -- consider Syphinx,Lucene,etc (3rd Party) all the fields needed in a SELECT are included in the INDEX.</tr>

ENGINE Differences

Discussion
RoTs </tr>

Optimizations,and not

Discussion
RoTs If you can arrange for rows to be "adjacent" to each other,then one disk fetch will bring in many rows (10x speedup). "Batched" INSERTs are where one INSERT statement has multiple rows. Nearly all of the performance benefit is in the first 100 rows; going beyond 1000 is really getting into 'diminishing returns'. Furthermore,in a Replication environment,a huge INSERT would cause the Slave to get 'behind'.  </tr>

PARTITIONing

Discussion
RoTs 1M rows     ⚈  No more than 50 PARTITIONs on a table (open,show table status,are impacted) (fixed in 5.6.6?)     ⚈  PARTITION BY RANGE is the only useful method.     ⚈  SUBPARTITIONs are not useful.     ⚈  The partition field should not be the field first in any key.     ⚈  It is OK to have an AUTO_INCREMENT as the first part of a compound key,or in a non-UNIQUE index. could INSERT a duplicate id if you explicitly provide the number.  </tr>

Memory Usage

Discussion
RoTs 1/sec,increase table_open_cache.     ⚈  Turn off the Query Cache. Type=off and size=0  </tr>

Character Sets

Discussion
RoTs utf8_general_ci > utf8_bin     ⚈  Debug stored data via HEX(col),LENGTH(col),CHAR_LENGTH(col)     ⚈  Do not use utf8 for hex or ascii strings (GUID,md5,ip address,country code,postal code,etc.) = CHAR_LENGTH(col): with European text '=' for latin1,'>' for utf8.  </tr>

Datatypes - Directly supported

Discussion
RoTs WHERE a.start < b.end AND a.end > b.start     ⚈  Don't be surprised by AUTO_INCREMENT values after uncommon actions.  More cacheable --> Faster. An AUTO_INCREMENT is very non-random,at least for inserting. Each new row will be on the 'end' of the table. That is,the last block is "hot spot". Thanks to caching very little I/O is needed for an AUTO_INCREMENT index. VARCHAR(255) for everything is tempting. And for "small" tables it won't hurt. For large tables one needs to consider what happens during the execution of complex SELECTs. If a "temporary" table is implicitedly generated,the VARCHAR will take 767 bytes in the temp table (2+3*255) bytes. 2=VAR overhead,3=utf8 expansion,255=your limit. A DELETE of the last row may or many not burn that AUTO_INCREMENT id. INSERT IGNORE burns ids because it allocates values before checking for duplicate keys. A Slave may see InnoDB ids arriving out of order (because transactions arrive in COMMIT order). A ROLLBACK (explicit or implicit) will burn any ids already allocated to INSERTs. REPLACE = DELETE + INSERT,so the INSERT comments apply to REPLACE. After a crash,the next id to be assigned may or may not be what you expect; this varies with Engine.  </tr>

Datatypes - Implicit

Discussion
RoTs Since GUID,UUID,MD5,and SHA1 are fixed length,VAR is not needed. If they are in hex,don't bother with utf8; use BINARY or CHAR CHARSET ascii. Images could be stored in BLOB (not TEXT). This better assures referential integrity (not accidentally deleting the metadata or image,but not both). On the other hand,it is clumsy. With files,an img tag can point directly to the image on disk.  </tr>

Hardware

Discussion
RoTs 8 cores degrade performance. (Changes coming in XtraDB,5.6,MariaDB,etc) (5.6 claims to be good to 48 cores - YMMV; 5.7 claims 64)     ⚈  A single connection will not use more than one core. Not even with UNION or PARTITION.     ⚈  Don't put a cache in front of a cache     ⚈  10x speed up when disk blocks are cached,so... Time a query twice -- first will get things cached,second will do no I/O     ⚈  Benchmark with "SELECT SQL_NO_CACHE ..." (to avoid Query cache)  </tr>

PXC / Galera

Discussion
RoTs or slower than traditional replication     ⚈  AUTO_INCREMENT values won't be consecutive     ⚈  Handle "critical reads" using wsrep_causal_reads     ⚈  ALTERs need to be handled differently (see RSU vs TOI)     ⚈  Lots of tricks are based on: remove from cluster + do stuff + add back to cluster     ⚈  Minimal HA: 1 node in each of 3 datacenters; one could be just a grabd  </tr>

Data Warehouse

Discussion
RoTs </tr>

Miscellany

Discussion
RoTs 10,you may be in serious trouble.     ⚈  SHOW PROCESSLIST with some threads "Locked" -- some other thread is hogging something.     ⚈  SHOW PROCESSLIST may fail to show the locking thread -- it is Sleeping,but not yet COMMITted.     ⚈  >90% CPU --> investigate queries/indexes. (The SlowLog also catches such.)     ⚈  >90% of one core -- since MySQL won't use multiple cores in a single connection,this indicates an inefficient query. (Eg,12% overall on an 8-core box is probably consuming one core.)     ⚈  >90% I/O -- tuning,overall schema design,missing index,etc.     ⚈  "NoSQL" is a catchy phrase looking for a definition. By the time NoSQL gets a definition,it will look a lot like an RDBMS solution. MariaDB 5.3's "Dynamic Columns" eats into a big excuse for "NoSQL".  </tr>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


安装开始...1.打开“https://dev.mysql.com/downloadsepo/yum/”下载Mysql源      将下载好的mysql源上传linux服务器 2.yumlocalinstallmysql80*#安装源 centos7中默认安装的是mariadb数据库,如果想要安装mysql,首先要移除mariadb;
安装Helm3#官网下载慢#wgethttps://get.helm.sh/helm-v3.5.4-linux-amd64.tar.gzwgethttp://qiniu.dev-share.top/helm-v3.5.4-linux-amd64.tar.gztar-zxvfhelm-v3.5.4-linux-amd64.tar.gzcplinux-amd64/helm/usr/local/bin#查看helmclient版本helmversion
通过Linux命令行启动用的指令:systemctlstartmariadb.service反馈:Failedtostartmariadb.service:Unitmariadb.servicenotfound.MariaDB简介MariaDB是MySQL的一个分支,MariaDB打算保持与MySQL的高度兼容性,确保具有库二进制奇偶校验的直接替换功能,以及与MySQLAPI和命令
InstallingMariaDBServer10.4TodeployMariaDBCommunityServer10.4onRHEL7orCentOS7,firstdownloadandusethe mariadb_repo_setup scripttoconfiguretheMariaDBrepositoriesforYUM:$sudoyuminstallwget$wgethttps://downloads.mariadb.com/
阅读目录一什么是存储引擎二mysql支持的存储引擎三使用存储引擎一什么是存储引擎mysql中建立的库--> 文件夹库中建立的表--> 文件现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型,处理表格用excel,处理图片
1、安装MariaDB安装命令yum-yinstallmariadbmariadb-server安装完成MariaDB,首先启动MariaDBsystemctlstartmariadb设置开机启动systemctlenablemariadb[root@node1~]#systemctlenablemariadbCreatedsymlinkfrom/etc/systemd/system/multi-user.target.wants/m
Centos7.5 刚安装的mariadb数据库报错报错:ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)原因一:没有启动数据库解决方法:systemctlstartmariadbsystemctlenablemariadb原因二:未知解决方法:[root@db01~]#
基于YUM安装的mariadb多实例.=================================================================1.yum安装mariadb-server包#yuminstallmariadb-server2.创建多实例对应的目录结构#mkdir/mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}-pv#tree/mysql//mysql/
一、系统环境[root@localhost~]#cat/etcedhat-releaseCentOSLinuxrelease7.6.1810(Core)二、mysql安装#yuminstallmysqlmysql-servermysql-devel安装mysql-server失败,如下图:[root@localhost~]#yuminstallmysql-serverLoadedplugins:fastestmirrorLoadingm
数据库的选择两大点是:开源和跨平台,满足这三点MySQL、MongoDB和MariaDB。其中MariaDB是MySQL的分支,也是它的进阶产品,未来很有可能替代MySQL。与MySQL相比较,MariaDB更强的地方在于:Maria 存储引擎PBXT存储引擎XtraDB 存储引擎FederatedX 存储引擎更快的复制查询处理线
使用Navicat连接数据库时出现了 HostxxxisnotallowedtoconnecttothisMariaDbserver的情况。发现了是因为授权的问题,使得连接权限受阻。所以,只需要进入数据库中,给予其权限即可。具体解决代码如下:[root@localhost~]#mysql-uroot-pEnterpassword:#首先进入mys
1.临时表当绘画结束时,临时表会自动销毁,无法用showtables查看临时表。MariaDB[jason]>createtemporarytabletmp(prochar(30),citychar(30));QueryOK,0rowsaffected(0.01sec)MariaDB[jason]>insertintotmpvalues('shanghai','shanghai');QueryOK,1
为了看阳光我来到世上数据库介绍数据库是一个存放数据的仓库,目前市面上最流行的数据库大致氛围的两种,一种叫做关系型数据库,一种叫做非关系型数据库,而关系型数据库近期流行的即为mysql,mysql原本是一个开源的数据库后被oracle公司收购,开始进行服务收费,因此,市面上大多数免费
1,Linux上的mysql MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松
启动Mariadb前提需安装mariadb-server-安装mariadb-serveryuminstall-ymariadb-server-启动服务systemctlstartmariadb.service-添加到开机启动systemctlenablemariadb.service-安全设置,以及修改数据库管理员密码mysql_secure_installation-启
1、查看是否安装及可用安装:yumlist|grepmaria2、安装maria(依赖其他几个安装包):yuminstallmariadb-server.x86_64输入'y'继续安装直到安装结束3、检查是否安装:4、安装完后登录提示错误:mysql-uroot-p是由于安装完但是并未启动maria服务5、启动maria有以下几种方法:system
通过yum安装mariadb,并配置MySQL主从服务器主服务器:192.168.10.11从服务器:192.168.10.12#!/bin/bash#====================================================#Author:Mr.Song#CreateDate:2019-02-21#Description:autoconfigMySQLmaster&slave#=====================
实验:实现基于SSL加密的主从复制实验步骤:环境:三台主机,一台CA:200,一台master:150,一台slave:100平时都是在CA上帮用户生成私钥,在服务器上做的1CA,master,slave的证书相关文件mkdir/etc/my.cnf.d/sslcd/etc/my.cnf.d/sslopensslgenrsa2048>cakey.pemopensslreq-new-x509-k
MariaDB[db1]>select*fromstudent;+----+------+-----+--------+-------+|id|name|age|gender|phone|+----+------+-----+--------+-------+|1|a|20|m|119||2|b|20|m|120||3|c|20|m|110
过年了,在老家闲余时间想敲敲代码,发现在安装mariaDb的时候一直报错错误信息:Service‘MySQL’(MySQL)Faildtostart,Verifythatyouhavesuffcientprivilegestostartsystemservices.服务的MySQL(MySQL)启动错误,确认你有权限启动系统服务。记得多年前在使用sqlserver数
Discussion