Tag Archive for 'MySQL'

Page 3 of 5

MySQL性能优化6 – 索引类型

MyISAM 存储引擎的 表的数据和索引是自动分开存储的,各自是独立的一个文件; InnoDB 存储引擎 的表的数据和索引是存储在同一个表空间里面,但可以有多个文件组成。

MySQL 中索引的存储类型目前只有两种( btree 和 hash ),具体和表的存储引擎相关: MyISAM 和 InnoDB 存储引擎都只支持 btree 索引; MEMORY/HEAP 存储引擎可以支持 hash 和 btree 索引。

MEMORY 存储引擎的表可以选择使用 BTREE 索引或者 HASH 索引,两种不同类型的索引各有其不同的适用范围。 HASH 索引有一些重要的特征需要在使用的时候特别注意,如下所示。 Continue reading ‘MySQL性能优化6 – 索引类型’

Share

MySQL性能优化5 – MySQL SQL Profiler 性能分析器

MySQL 的 SQL 性能分析器主要用途是显示 SQL 执行的整个过程中各项资源的使用情况。分析器可以更好的展示出不良 SQL 的性能问题所在。

下面我们举例介绍一下 MySQL SQL Profiler 的使用方法: Continue reading ‘MySQL性能优化5 – MySQL SQL Profiler 性能分析器’

Share

MySQL性能优化4 – 分析 SQL 执行计划

通过以上步骤查询到效率低的 SQL 语句 后,可以通过 EXPLAIN 或者 DESC 命令获取 MySQL 如何执行 SELECT 语句的信息,包括在 SELECT 语句执行过程中表如何连接和连接的顺序,比如我们想计算 2006 年所有公司的销售额,需要关联 sales 表和 company 表,并且对 profit 字段做求和(sum)操作,相应 SQL 的执行计划如下: Continue reading ‘MySQL性能优化4 – 分析 SQL 执行计划’

Share

MySQL性能优化3 – 定位效率底的 SQL

一般通过以下两种方式定位执行效率较低的 SQL 语句。

通过慢查询日志定位那些执行效率较低的 SQL 语句,用 --log-slow-queries[=file_name]选项启动时, mysqld 会 写一个包含所有执行时间超过  long_query_time 秒的 SQL 语句的日志文件,通过查看这个日志文件定位效率较低的 SQL 。
慢查询日志在查询结束以后才纪录,所以在应用反映执行效率出现问题的时候查询慢查询日志并不能定位问题,可以使用 show processlist 命令查看当前 MySQL 在进行的线程,包括线程的状态、是否锁表等,可以实时地查看 SQL 的 执行情况,同时对一些锁表操作进行优化。 Continue reading ‘MySQL性能优化3 – 定位效率底的 SQL’

Share

MySQL性能优化2 – 了解 SQL 执行频率

MySQL 客户端连接成功后,通过 show [session|global]status 命令 可以提供服务器状态信息,也可以在操作系统上使用 mysqladmin extended-status 命令获得这些消息。 show [session|global] status 可以根据需要加上参数“ session ”或者“ global ”来显示 session 级(当前连接)的统计结果和 global 级(自数据库上次启动至今)的统计结果。如果不写,默认使用参数是“ session ”。 Continue reading ‘MySQL性能优化2 – 了解 SQL 执行频率’

Share

MySQL性能优化1 – 简介

简单的说, SQL 优化就是通过使用一些工具和方法,找到那些问题 SQL, 并对其执行计划进行分析和改进,从而改善和提高这些问题 SQL 的执行效率,使他们更快、更好的执行。

SQL 优化中,了解和使用 EXPLAIN( 执行计划 ) ,是非常重要的。 EXPLAIN 可以告诉你 SQL 语句在 MySQL 中是如何执行的。 Continue reading ‘MySQL性能优化1 – 简介’

Share

ubuntu下MySQL服务器更改目录后问题

1
2
3
4
5
6
100811 18:42:06  InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name ./ibdata1
InnoDB: File operation call: 'open'.
InnoDB: Cannot continue operation.

修改下面文件中得权限列表可解决问题。

1
/etc/apparmor.d/usr.sbin.mysqld
1
2
/web/data/mysql/ r,
/web/data/mysql/** rwk,
Share

How to Repair a Crashed MySQL Table

Today, I had a server crash when the /var partition filled up while reindexing. I attempted to clear out extra log files and such but df was still showing greater than 100% utilization. Checking /var/db/mysql showed I had one table that was taking up too much space. du -d 1 -h is a very useful command when tracking down the largest folder usage on partition.

Jul 21 07:39:06 plab2catsg01 kernel: pid 36500 (mysqld), uid 88 inumber 329992 o
n /var: filesystem full
Jul 21 07:39:22 plab2catsg01 kernel: pid 36500 (mysqld), uid 88 inumber 353413 o
n /var: filesystem full
Jul 21 07:39:23 plab2catsg01 kernel: pid 36500 (mysqld), uid 88 inumber 353288 o
n /var: filesystem full

I attempted to stop MySQL by executing /usr/local/etc/rc.d/mysql-server stop and unfortunately, it kept looping while trying to stop the process. I was eventually forced to reboot the server.

On reboot, I thought all was well but then I found the Eventum application I use wasn't showing support emails. I checked the MySQL log files:

plab2catsg01# tail -f plab2catsg01.ten-net.net.err
...
090721  9:15:54 [ERROR] /usr/local/libexec/mysqld: Table './eventum/eventum_mail
_queue' is marked as crashed and last (automatic?) repair failed
090721  9:15:54 [ERROR] /usr/local/libexec/mysqld: Table './eventum/eventum_mail
_queue' is marked as crashed and last (automatic?) repair failed
090721  9:15:55 [ERROR] /usr/local/libexec/mysqld: Table './eventum/eventum_mail
_queue' is marked as crashed and last (automatic?) repair failed
090721  9:15:55 [ERROR] /usr/local/libexec/mysqld: Table './eventum/eventum_mail
_queue' is marked as crashed and last (automatic?) repair failed
...

Continue reading 'How to Repair a Crashed MySQL Table'

Share

Use MySQL Regexp Sample

1
DELETE FROM `google_data` WHERE site_id=4 AND url REGEXP '\.php\\?matchid=[0-9]+$'
Share

Range number in MySQL

To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement:

SELECT FLOOR(7 + (RAND() * 5));

Share