MySQL select now()的性能问题

问题描述

localtime函数会去调用/etc/localtime文件,对于大量并发的select now(),可以观察到明显的瓶颈(下面红色标注的函数)

26 __lll_lock_wait_private(libc.so.6),_L_lock_2164(libc.so.6),__tz_convert(libc.so.6),Time_zone_system::gmt_sec_to_TIME(tztime.cc:1079),gmt_sec_to_TIME(tztime.h:60),set_datetime(tztime.h:60),Item_func_now::fix_length_and_dec(tztime.h:60),Item_func::fix_fields(item_func.cc:231),setup_fields(sql_base.cc:8229),JOIN::prepare(sql_resolver.cc:168),mysql_prepare_select(sql_select.cc:1054),mysql_select(sql_select.cc:1054),handle_select(sql_select.cc:110),execute_sqlcom_select(sql_parse.cc:5544),mysql_execute_command(sql_parse.cc:3033),mysql_parse(sql_parse.cc:6729),dispatch_command(sql_parse.cc:1634),do_handle_one_connection(sql_connect.cc:982),handle_one_connection(sql_connect.cc:898),pfs_spawn_thread(pfs.cc:1858),start_thread(libpthread.so.0),clone(libc.so.6)

localtime调用会去访问本地文件,引起底层库级别的锁冲突(类似bug#72701),对于高性能服务器,应该尽量避免调用localtime函数。之前FB的Domas也写了篇文章来描述这个问题

Workaround

导入时区信息:

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -uroot -S /u01/my3306/run/mysql.sock mysql

查看本机时区配置:

$cat /etc/sysconfig/clock

ZONE=”Asia/Shanghai”

然后以以下任意一种方式设置:

1).

mysql> set global time_zone=’Asia/Shanghai’;

Query OK, 0 rows affected (0.00 sec)

2).在不导入时区配置的情况下(不调用mysql_tzinfo_to_sql ),直接设置时区:

mysql> set global time_zone = ‘+8:00′;

Query OK, 0 rows affected (0.00 sec)

3).在配置文件里添加:

default-time-zone=’Asia/Shanghai’

测试

主要测试SELECT NOW()的效率

mysqlslap –no-defaults -uxx –create-schema=test -h$host  -P$port –number-of-queries=1000000000 –concurrency=100  –query=’select now()’

system time_zone: 15W QPS

显式指定中国时区“Asia/Shangehai”: 34W QPS

原创文章,转载请注明: 转载自Simple Life

本文链接地址: MySQL select now()的性能问题

Post Footer automatically generated by wp-posturl plugin for wordpress.


Comments

Leave a Reply

Your email address will not be published. Name and email are required


Current month ye@r day *