您好,欢迎访问一九零五行业门户网

Monty说MySQL的优化(七)_MySQL

三十一、事务的例子
myiasm表如何进行事务处理:
mysql> lock tables trans read, customer write;
mysql> select sum(value) from trans where customer_id=some_id;
mysql> update customer set total_value=sum_from_previous_statement
where customer_id=some_id;
mysql> unlock tables;
bdb表如何进行事务:
mysql> begin work;
mysql> select sum(value) from trans where customer_id=some_id;
mysql> update customer set total_value=sum_from_previous_statement
where customer_id=some_id;
mysql> commit;
注意你可以通过下列语句回避事务:
update customer set value=value+new_value where customer_id=some_id;
三十二、使用replace的例子
replace的功能极像insert,除了如果一条老记录在一个唯一索引上具有与新纪录相同的值,那么老记录在新纪录插入前则被删除。不使用 select 1 from t1 where key=#
if found-row
lock tables t1
delete from t1 where key1=#
insert into t1 values (...)
unlock tables t1;
endif
而用
replace into t1 values (...)
三十三、一般技巧
使用短主键。联结表时使用数字而非字符串。
当使用多部分键码时,第一部分应该时最常用的部分。
有疑问时,首先使用更多重复的列以获得更好地键码压缩。
如果在同一台机器上运行mysql客户和服务器,那么在连接mysql时则使用套接字而不是tcp/ip(这可以提高性能7.5%)。可在连接mysql服务器时不指定主机名或主机名为localhost来做到。
如果可能,使用--skip-locking(在某些os上为默认),这将关闭外部锁定并将提高性能。
使用应用层哈希值而非长键码:
select * from table_name where hash=md5(concat(col1,col2)) and
col_1='constant' and col_2='constant'
在文件中保存需要以文件形式访问的blob,在数据库中只保存文件名。
删除所有行比删除一大部分行要快。
如果sql不够快,研究一下访问数据的较底层接口。
三十四、使用mysql 3.23的好处
myisam:可移植的大表格式
heap:内存中的表
berkeley db:支持事务的表。
众多提高的限制
动态字符集
更多的status变量
check和repair表
更快的group by和distinct
left join ... if null的优化
create table ... select
create temporary table_name (...)
临时heap表到myisam表的自动转换
复制
mysqlhotcopy脚本
三十五、正在积极开发的重要功能
改进事务处理
失败安全的复制
正文搜索
多个表的删除(之后完成多个表的更新)
更好的键码缓存
原子rename (rename table foo as foo_old, foo_new as foo)
查询高速缓存
merge tables
一个更好的gui客户程序
其它类似信息

推荐信息