mysql 符合索引,未建立以前用时1.72s,建立一个符合索引后,反而用时2.44s,时间反而更长,是哪的问题呢,求解。。

以下是表结构和mysql explain 后的结果:

mysql> show create table new_bind_mp_weibo_user_info_0;
+----+| Table   | Create Table      |+---+
| new_bind_mp_weibo_user_info_0 | CREATE TABLE `new_bind_mp_weibo_user_info_0` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `weiboid` varchar(20) NOT NULL,
  `weiboun` varchar(100) NOT NULL,
  `mpnumber` varchar(11) NOT NULL,
  `apmac` varchar(100) NOT NULL,
  `bindtime` datetime NOT NULL,
  `source` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `search_key` (`bindtime`)
) ENGINE=MyISAM AUTO_INCREMENT=2768 DEFAULT CHARSET=utf8 | 
+-------------------------------+-----------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table weibo_user_visit_info_0; +—————–+| Table | Create Table |+———————————+ | weibo_user_visit_info_0 | CREATE TABLE weibo_user_visit_info_0 ( id int(11) NOT NULL AUTO_INCREMENT, weiboid varchar(20) NOT NULL, weiboun varchar(50) NOT NULL, apmac varchar(100) DEFAULT NULL, usermac varchar(100) DEFAULT NULL, mpnumber varchar(11) DEFAULT NULL, visittime datetime NOT NULL, source varchar(255) NOT NULL, PRIMARY KEY (id), KEY weiboid (weiboid) ) ENGINE=MyISAM AUTO_INCREMENT=17952 DEFAULT CHARSET=utf8 | +————————-+——————————————–+ 1 row in set (0.00 sec)

mysql> alter table weibo_user_visit_info_0 add index search_index (apmac(17),visittime); Query OK, 17944 rows affected (0.21 sec) Records: 17944 Duplicates: 0 Warnings: 0 mysql> explain select sql_no_cache count(distinct(a.mpnumber)) as mpnumber, date_format(a.bindtime,’%Y-%m-%d’) as gDate from new_bind_mp_weibo_user_info_0 a left join weibo_user_visit_info_0 b on a.mpnumber=b.mpnumber and a.apmac=b.apmac where b.id is NULL and a.apmac in (select ap_mac from newwifi.business_installedAP_info where businessid=100064) and a.bindtime between ‘2012-03-01’ and ‘2012-03-23 23:59:59’ group by gDate order by gDate; +—-+——————–+—————————+——-+—————+————–+———+—————————-+——+—————————–+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +—-+——————–+—————————+——-+—————+————–+———+—————————-+——+—————————–+ | 1 | PRIMARY | a | range | search_key | search_key | 8 | NULL | 394 | Using where; Using filesort | | 1 | PRIMARY | b | ref | search_index | search_index | 54 | newwifi_visit_info.a.apmac | 142 | Using where; Not exists | | 2 | DEPENDENT SUBQUERY | business_installedAP_info | ref | search_key | search_key | 306 | const,func | 1 | Using index | +—-+——————–+—————————+——-+—————+————–+———+—————————-+——+—————————–+ 3 rows in set (0.00 sec)

mysql> select sql_no_cache count(distinct(a.mpnumber)) as mpnumber, date_format(a.bindtime,’%Y-%m-%d’) as gDate from new_bind_mp_weibo_user_info_0 a left join weibo_user_visit_info_0 b on a.mpnumber=b.mpnumber and a.apmac=b.apmac where b.id is NULL and a.apmac in (select ap_mac from newwifi.business_installedAP_info where businessid=100064) and a.bindtime between ‘2012-03-01’ and ‘2012-03-23 23:59:59’ group by gDate order by gDate; +———-+————+ | mpnumber | gDate | +———-+————+ | 4 | 2012-03-01 | | 10 | 2012-03-02 | | 3 | 2012-03-03 | | 10 | 2012-03-04 | | 12 | 2012-03-05 | | 10 | 2012-03-08 | | 4 | 2012-03-09 | | 14 | 2012-03-12 | | 14 | 2012-03-13 | | 7 | 2012-03-14 | | 12 | 2012-03-15 | | 8 | 2012-03-16 | | 7 | 2012-03-17 | | 11 | 2012-03-18 | | 11 | 2012-03-19 | | 8 | 2012-03-20 | | 8 | 2012-03-21 | | 9 | 2012-03-22 | +———-+————+ 18 rows in set (2.44 sec)

mysql> alter table weibo_user_visit_info_0 drop index search_index; Query OK, 17944 rows affected (0.13 sec) Records: 17944 Duplicates: 0 Warnings: 0

mysql> explain select sql_no_cache count(distinct(a.mpnumber)) as mpnumber, date_format(a.bindtime,’%Y-%m-%d’) as gDate from new_bind_mp_weibo_user_info_0 a left join weibo_user_visit_info_0 b on a.mpnumber=b.mpnumber and a.apmac=b.apmac where b.id is NULL and a.apmac in (select ap_mac from newwifi.business_installedAP_info where businessid=100064) and a.bindtime between ‘2012-03-01’ and ‘2012-03-23 23:59:59’ group by gDate order by gDate; +—-+——————–+—————————+——-+—————+————+———+————+——-+———————————————-+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +—-+——————–+—————————+——-+—————+————+———+————+——-+———————————————-+ | 1 | PRIMARY | a | range | search_key | search_key | 8 | NULL | 394 | Using where; Using temporary; Using filesort | | 1 | PRIMARY | b | ALL | NULL | NULL | NULL | NULL | 17944 | Using where; Not exists | | 2 | DEPENDENT SUBQUERY | business_installedAP_info | ref | search_key | search_key | 306 | const,func | 1 | Using index | +—-+——————–+—————————+——-+—————+————+———+————+——-+———————————————-+ 3 rows in set (0.00 sec) mysql> select sql_no_cache count(distinct(a.mpnumber)) as mpnumber, date_format(a.bindtime,’%Y-%m-%d’) as gDate from new_bind_mp_weibo_user_info_0 a left join weibo_user_visit_info_0 b on a.mpnumber=b.mpnumber and a.apmac=b.apmac where b.id is NULL and a.apmac in (select ap_mac from newwifi.business_installedAP_info where businessid=100064) and a.bindtime between ‘2012-03-01’ and ‘2012-03-23 23:59:59’ group by gDate order by gDate; +———-+————+ | mpnumber | gDate | +———-+————+ | 4 | 2012-03-01 | | 10 | 2012-03-02 | | 3 | 2012-03-03 | | 10 | 2012-03-04 | | 12 | 2012-03-05 | | 10 | 2012-03-08 | | 4 | 2012-03-09 | | 14 | 2012-03-12 | | 14 | 2012-03-13 | | 7 | 2012-03-14 | | 12 | 2012-03-15 | | 8 | 2012-03-16 | | 7 | 2012-03-17 | | 11 | 2012-03-18 | | 11 | 2012-03-19 | | 8 | 2012-03-20 | | 8 | 2012-03-21 | | 9 | 2012-03-22 | +———-+————+ 18 rows in set (1.72 sec)


阅读全文

我们在写数据库程序的时候,经常会需要获取某个表中的最大序号数,或者刚插入的数据的ID值。

一般情况下获取刚插入的数据的id,使用select max(id) from table 是可以的。

但在多线程,高并发的情况下,就不行了。

开始的时候我想的是使用mysql_insert_id(),不知道会不会在并发的时候产生影响,查询了下手册,也是才发现,是根据connection来的,不同用户间不会产生影响。所以也不用去想先把表锁起来,插入取得ID值后再解锁。直接正常插入,然后取值,即可。

int mysql_insert_id ([ resource $link_identifier ] )

mysql_insert_id()返回给定的 link_identifier中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号。如果没有指定 link_identifier,则使用上一个打开的连接。

如果上一查询没有产生 AUTO_INCREMENT 的值,则 mysql_insert_id()返回 0。如果需要保存该值以后使用,要确保在产生了值的查询之后立即调用 mysql_insert_id()。

warning: mysql_insert_id() 将 MySQL 内部的 C API 函数 mysql_insert_id()的返回值转换成 long(PHP 中命名为 int)。如果 AUTO_INCREMENT 的列的类型是 BIGINT,则 mysql_insert_id()返回的值将不正确。可以在 SQL 查询中用 MySQL 内部的 SQL 函数 LAST_INSERT_ID() 来替代。

此外,还可以通过QUERY两个SQL语句来执行:

--1 ,查询 LAST_INSERT_ID()
mysql> select LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                4 | 
+------------------+
1 row in set (0.00 sec)

–2 查询 @@IDENTITY mysql> select @@IDENTITY; +————+ | @@IDENTITY | +————+ | 4 | +————+ 1 row in set (0.00 sec)


阅读全文

首先说一下mysql的数值类型,MySQL支持所有标准SQL数值数据类型。这些类型包括严格数值数据类型(INTEGER、SMALLINT、DECIMAL和NUMERIC),以及近似数值数据类型(FLOAT、REAL和DOUBLE PRECISION)。关键字INT是INTEGER的同义词,关键字DEC是DECIMAL的同义词。

BIT数据类型保存位字段值,并且支持MyISAM、MEMORY、InnoDB和BDB表。

作为SQL标准的扩展,MySQL也支持整数类型TINYINT、MEDIUMINT和BIGINT。下面的表显示了需要的每个整数类型的存储和范围。

<td>
  <strong>字节</strong>
</td>

<td> <strong>最小值</strong> </td>

<td> <strong>最大值</strong> </td>

<td>
</td>

<td> <strong>(带符号的/无符号的)</strong> </td>

<td> <strong>(带符号的/无符号的)</strong> </td>

<td>
  1
</td>

<td> -128 </td>

<td> 127 </td>

<td>
</td>

<td> </td>

<td> 255 </td>

<td>
  2
</td>

<td> -32768 </td>

<td> 32767 </td>

<td>
</td>

<td> </td>

<td> 65535 </td>

<td>
  3
</td>

<td> -8388608 </td>

<td> 8388607 </td>

<td>
</td>

<td> </td>

<td> 16777215 </td>

<td>
  4
</td>

<td> -2147483648 </td>

<td> 2147483647 </td>

<td>
</td>

<td> </td>

<td> 4294967295 </td>

<td>
  8
</td>

<td> -9223372036854775808 </td>

<td> 9223372036854775807 </td>

<td>
</td>

<td> </td>

<td> 18446744073709551615 </td>

类型
TINYINT
SMALLINT
MEDIUMINT
INT
BIGINT

Mysql类型关键字后面的括号内指定整数值的显示宽度(例如,INT(4))。该可选显示宽度规定用于显示宽度小于指定的列宽度的值时从左侧填满宽度。显示宽度并不限制可以在列内保存的值的范围,也不限制超过列的指定宽度的值的显示。

当结合可选扩展属性ZEROFILL使用时, 默认补充的空格用零代替。例如,对于声明为INT(5) ZEROFILL的列,值4检索为00004。请注意如果在整数列保存超过显示宽度的一个值,当MySQL为复杂联接生成临时表时会遇到问题,因为在这些情况下MySQL相信数据适合原列宽度。

所有整数类型可以有一个可选(非标准)属性UNSIGNED。当你想要在列内只允许非负数和该列需要较大的上限数值范围时可以使用无符号值。

所以int(10)与int(11)后的括号中的字符表示显示宽度,整数列的显示宽度与mysql需要用多少个字符来显示该列数值,与该整数需要的存储空间的大小都没有关系,int类型的字段能存储的数据上限还是2147483647(有符号型)和4294967295(无符号型)。


阅读全文

由于微博API要求从2011年12月26日起,所有请求必须加入请求者的IP。由于从内网发起请求,最后header中获得的IP为10打头的内网IP,导致认证失败。

修改sae的SaeTClientV2类的http()方法中检测下,如果IP是10打头的内网IP,强制转换header中的saeremoteip为一个外网IP。

$remote_addr = $_SERVER['REMOTE_ADDR'];
		if (substr($remote_addr,0,3) === '10.') {
			$remote_addr = '61.135.152.203';
			$headers[] = "SaeRemoteIP: " . $remote_addr;
		}

阅读全文

mysql命令行中执行多行命令时,如果前边输入的命令发生错误,是无法返回修改的,但是可以通过输入c来取消前边的输入,但是这时如果前边输入的东西很多,直接取消又很可惜的话,可以通过p来打印出前边的命令,复制下来去修改,然后输入c取消来重新输入命令。

mysql> select 8 from a
    -> where 1=1
    ->  and 1=0
    -> p
#输入p后,打印出了前边的命令。
--------------
select 8 from a
where 1=1
 and 1=0
--------------
#依然进入->状态,等待输入,输入c,取消当前输入。
    -> c
mysql>

阅读全文

Memcache 命令行用法

1、启动Memcache 常用参数

memcached 1.4.3
-p <num>      设置端口号(默认不设置为: 11211)
-U <num>      UDP监听端口 (默认: 11211, 0 时关闭)  

-l <ip_addr> 绑定地址 (默认:所有都允许,无论内外网或者本机更换IP,有安全隐患,若设置为127.0.0.1就只能本机访问) -d 独立进程运行 -u <username> 绑定使用指定用于运行进程 <username> -m <num> 允许最大内存用量,单位M (默认: 64 MB) -P <file> 将PID写入文件<file>,这样可以使得后边进行快速进程终止, 需要与 -d 一起使用

如:

在linux下:./usr/local/bin/memcached -d -u jb-mc -l 192.168.1.197 -m 2048 -p 12121

在window下:d:App_Servmemcachedmemcached.exe -d RunService -l 127.0.0.1 -p 11211 -m 500

在windows下注册为服务后运行:

sc.exe create jb-Memcached binpath= “d:App_Servmemcachedmemcached.exe -d RunService -p 11211 -m 500″ start= auto

net start jb-Memcached

 


阅读全文

mysql的like查询默认是不区分大小写的 如:

select * from table_name where a like 'a%'
select * from table_name where a like 'A%'

效果是一样的。

要让mysql的like查询区分大小写,可以这样:

select * from table_name where binary a like 'a%'
select * from table_name where binary a like 'A%'

也可以在建表时,加以标识binary,也可实现like查询时区分大小写:

create table table_name(
     a varchar(20) binary 
)

 


阅读全文

作者的图片

DigDeeply

Technology Stack: PHP/Openresty/GoLang, and so on…

Web Development Engineer

Beijing China