sql学习笔记(18)-----------数据库创建过程(七)

2015-07-24 08:11:21 · 作者: · 浏览: 20
me/mysql/film_test4.txt'into table film_test4;?
Query OK, 1587168 rows affected (31.16 sec)?
Records:1587168 Deleted:0 Skipped:0 Warnings:0
(2)关闭唯一性效验可以提高导入效率
?
在导入数据前先执行set unique_checks=0,关闭唯一性效验,在导入结束后执行set unique_checks=1,恢复唯一性效验,可以提高导入效率。
?
?
--当unique_checks=1时
?
mysql> load data infile ‘/home/mysql/film_test3.txt'into table film_test4;?
Query OK,1587168 rows affected (22.92 sec)?
Records:1587168 Deleted:0 Skipped:0 Warnings:0
--当unique_checks=0时
?
mysql> load data infile ‘/home/mysql/film_test3.txt'into table film_test4;?
Query OK,1587168 rows affected (19.92 sec)?
Records:1587168 Deleted:0 Skipped:0 Warnings:0
(3)关闭自动提交可以提高导入效率
?
在导入数据前先执行set autocommit=0,关闭自动提交事务,在导入结束后执行set autocommit=1,恢复自动提交,可以提高导入效率。
?
?
--当autocommit=1时
?
mysql> load data infile ‘/home/mysql/film_test3.txt'into table film_test4;?
Query OK,1587168 rows affected (22.92 sec)?
Records:1587168 Deleted:0 Skipped:0 Warnings:0
--当autocommit=0时
?
mysql> load data infile ‘/home/mysql/film_test3.txt'into table film_test4;?
Query OK,1587168 rows affected (20.87 sec)?
Records:1587168 Deleted:0 Skipped:0 Warnings:0
优化insert语句
尽量使用多个值表的insert语句,这样可以大大缩短客户与数据库的连接、关闭等损耗。
?
可以使用insert delayed(马上执行)语句得到更高的效率。
?
将索引文件和数据文件分别存放不同的磁盘上。
?
可以增加bulk_insert_buffer_size 变量值的方法来提高速度,但是只对MyISAM表使用当从一个文件中装载一个表时,使用LOAD DATA INFILE。这个通常比使用很多insert语句要快20倍。
?
优化group by语句
如果查询包含group by但用户想要避免排序结果的损耗,则可以使用使用order by null来禁止排序:
?
如下没有使用order by null来禁止排序
?
mysql> explain select id,sum(moneys) from sales2 group byid\G?
*************************** 1. row ***************************?
id: 1?
select_type: SIMPLE?
table: sales2?
type: ALL?
possible_keys: NULL?
key: NULL?
key_len: NULL?
ref: NULL?
rows: 1000?
Extra: Using temporary;Using filesort?
1 row inset (0.00 sec)
如下使用order by null的效果:
?
mysql> explain select id,sum(moneys) from sales2 group byid order by null\G?
*************************** 1. row ***************************?
id: 1?
select_type: SIMPLE?
table: sales2?
type: ALL?
possible_keys: NULL?
key: NULL?
key_len: NULL?
ref: NULL?
rows: 1000?
Extra: Using temporary?
1 row inset (0.00 sec)
优化嵌套查询
下面是采用嵌套查询的效果(可以使用更有效的链接查询(Join)替代)。
?
mysql> explain select * from sales2 where company_id notin(selectid?
from company2)\G?
*************************** 1. row ***************************?
id: 1?
select_type: SIMPLE?
table: sales2?
type: ALL?
possible_keys: NULL?
key: NULL?
key_len: NULL?
ref: NULL?
rows: 1000?
Extra: Using where?
1 row inset (0.00 sec)
*************************** 2. row ***************************?
id: 2?
select_type: SIMPLE?
table: company2?
type: index_subquery?
possible_keys: ind_company2_id?
key: ind_company2_id?
key_len: 5?
ref: func?
rows: 2?
Extra: Using index?
1 row inset (0.00 sec)
下面是使用更有效的链接查询(Join)
?
mysql> explain select * from sales2 leftjoin company2 on?
sales2.company_id = company2.idwhere sales2.company_id is null\G?
*************************** 1. row ***************************
id: 1?
select_type: SIMPLE?
table: sales2?
type: ALL?
possible_keys: i