@[TOC]重庆邮电大学经典商城实战
慢查询和show processlist,explain等优化sql
create table city_innodb(
city_id int NOT NULL AUTO_INCREMENT,
city_name varchar(50) NOT NULL,
country_id int NOT NULL,
primary key(city_id),
key idx_fk_country_id(country_id),
constraint ‘fk_city_country’ foreign key(country_id) references country_innodb(country_id) on delete restrict on update cascade
)engine=innodb default charset=utf8;
desc city_innodb;
desc country_innodb;
restrict 和 no action相同,是指限制在子表有关联记录的情况下,父表不能更新;
cascade 表示父表在更新或者删除时,更新或者删除子表对应的记录;
– 查询数据库中哪种操作比较频繁
show status like ‘Com_______’;
show global status like ‘Com_______’;
show global status like ‘Innodb_rows_%’;
– 定位低效率执行Sql
慢查询日志:通过慢查询日志定位那些执行效率低的SQL语句,用–log
–slow-queries[=file_name]选项启动时,mysqld写一个包含所有执行时间超过long_query_time秒的sql语句的日志文件。具体可以查看本书第26章中日志管理的相关部分。
show processlist:慢查询日志在查询结束以后才记录,所以在应用反映执行效率出现问题的时候查询慢查询日志并不能定位问题,可以使用show processlist命令查看当前MySQL在进行的线程,包括线程的状态,是否锁表等,可以实时地查看SQL的执行情况,同时对一些锁表操作进行优化。