MySQL最佳化之二三事

來源:互聯網
上載者:User
SQL語句之優劣

SELECT * FROM node_revisions WHERE nid IN ( SELECT max(nid) FROM node )
Better
SELECT @maxid :=max(nid) FROM node;
SELECT * FROM node_revisions WHERE nid = @maxid

SELECT * FROM node WHERE nid > 10 OR created > 1209793459;
Better (If either of nid and created is not index)
SELECT * FROM node WHERE nid > 10
UNION
SELECT * FROM node WHERE create > 1209793459

SELECT * FROM node WHERE nid IN (SELECT nid FROM term_node WHERE tid <10 )
Better(using subquery is not a good choice in common)
SELECT n.* FROM node n LEFT JOIN term_node t ON t.nid = n.nid WHERE tid < 10

Group By 之最佳化

The results of group by will be sort as group by column(s) in default, so if you don’t want to sort result, add ORDER BY NULL:
SELECT count(nid) FROM node group by vid
explain:
| id|select_type|table|type| possible_keys| key|key_len|ref |rows| Extra                     
| 1|SIMPLE    | node| ALL| NULL           |NULL|NULL| NULL| 23 | Using temporary;Using filesort

SELECT count(nid) FROM node group by vid ORDER BY NULL
explain:
| id |  select_type | table  | type  | possible_keys | key    | key_len | ref | rows | Extra 
| 1  |  SIMPLE    | node  | ALL  | NULL                 | NULL | NULL  | NULL |   23 | Using temporary

Sometimes group by sql statement is too slow in large table,the problem maybe is the variable tmp_table_size too small, which is 32M in default, so you need change it to the bigger value.

CREATE INDEX之最佳化

When you create index on varchar or char column, partial index is better than all index, especially in large column:
CREATE INDEX name_index ON USERS (name (10));
It will create index only the first 10 characters.

MYSQL Server Configuration

Add configuration for mysql section
[mysqld]
skip-name-resolve  #Don’t resolve hostnames

Compile MySQL on Linux

config.mysql.sh
#!/bin/bash

exe=configure   


$exe --prefix=/opt/mysql --localstatedir=/opt/mysql/data --with-mysqld-user=mysql --sysconfdir=/opt/mysql/etc --with-unix-socket-path=/opt/mysql/temp/mysql.sock --with-tcp-port=3307 --with-charset=utf8 --with-extra-charsets=all --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --enable-assembler --enable-thread-safe-client --with-pthread --without-debug --without--isam

#configure的相關參數:
#--prefix=/home/mysql/ \指定安裝目錄
#--without-debug \去除debug模式
#--with-extra-charsets=gbk,gb2312,utf8 \添加gbk,gb2312,utf8中文字元支援
#--with-pthread \強制使用pthread庫(posix線程庫)
#--enable-assembler \使用一些字元函數的彙編版本
#--enable-thread-safe-client \以線程方式編譯用戶端
#--with-client-ldflags=-all-static \以純靜態方式編譯用戶端
#--with-mysqld-ldflags=-all-static \以純靜態方式編譯服務端
#--without-isam \去掉isam表類型支援,現在很少用了,isam表是一種依賴平台的表
#--without-innodb \去掉innodb表支援,innodb是一種支援交易處理的表,適合企業級應用

Problems In MySQL  

遠程無法訪問

1.mysql>GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY "1234";

2.$sudo gedit /etc/mysql/my.cnf
老的版本中
>skip-networking => # skip-networking
新的版本中
>bind-address=127.0.0.1 => bind-address= 你機器的IP
127.0.0.1知允許本地訪問

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.