mysql join查询时,参与join的字段字符集编码不同,对性能影响是巨大的

本文一直以来都只是个标题,在三个月后的今天,熬夜补上内容。

直接上代码:

建utf-8编码的表 t1:

CREATE TABLE IF NOT EXISTS `t1` (
  `name` varchar(50) NOT NULL DEFAULT '',
  KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

随便插入些数据,数量大一点,后面实验结果更清晰,偷个懒,构造随机字符串插入语句

insert into t1(name) 
select concat(
char(round((rand())*25)+97),
char(round((rand())*25)+65),
char(round((rand())*25)+65),
char(round((rand())*25)+97),
char(round((rand())*25)+65),
char(round((rand())*25)+65),
char(round((rand())*25)+97),
char(round((rand())*25)+65)
)

每次执行插入一条记录,用你熟悉的脚本(python,php,shell等都行)写个循环,执行一万次以上。

将该表复制成一个新表t2,删除一部分数据,1000条左右即可。(推荐使用phpMyAdmin)

再将t2复制为t3,并将字段改为gb2312编码。

使用一个left join语句,写一个语句,查出t2/t3比t1少了哪些记录。

语句很简单,如下:

SELECT SQL_NO_CACHE t1.name, t2.name
FROM t1
LEFT JOIN t2 ON t1.name = t2.name
WHERE t2.name IS NULL 
LIMIT 0 , 30

注意加入 SQL_NO_CACHE ,禁用mysql缓存。

先看编码一致的t2表,phpMyAdmin里执行结果:

显示行 0 - 29 ( 1,129 总计, 查询花费 0.0010 秒)

平均耗时大概为0.0010秒

SELECT SQL_NO_CACHE t1.name, t3.name
FROM t1
LEFT JOIN t3 ON t1.name = t3.name
WHERE t2.name IS NULL 
LIMIT 0 , 30

phpMyAdmin执行结果:

显示行 0 - 29 ( 30 总计, 查询花费 0.1871 秒)

差两个数量级!

查询语句解释:

t2

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL name 152 NULL 11129 Using index
1 SIMPLE t2 ref name name 152 test.t1.name 1 Using where; Using index; Not exists

t3

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL name 152 NULL 11129 Using index
1 SIMPLE t3 index NULL name 102 NULL 10000 Using where; Using index; Not exists

附带上本次实验的三个表及数据
实验用表导出备份test.sql

 

使用wget抓取网页内容,常规用法及有用参数

环境:linux系统(各种发行版皆可),windows下强烈推荐cygwin环境

常规用法:

  1. 直接下载一个文件,默认按最后一级文件名保存到本地: wget {your-url}
  2. 如需自定义保存文件名则增加参数 -O 指定保留文件路径: wget -O {save-file-name} {your-url}
  3. 下载一批文件,把文件路径放到一个文本文件中,每行一个: wget -i {file-name-of-your-filelist}
  4. 下载一个页面及页面中图片等元素,wget -p {your-url}   ,即增加参数 --page-requisites (缩写为-p)
  5. 按网址中目录及文件名存储下载文件,即与源站同样的目录结构,增加参数 --force-directories-x

一些有用参数设置

  1. 指定user-agent 参数: -U {agent-string}--user-agent={agent-string}
  2. 批量下载时设置请求间隔:--wait={秒数}-w {秒数}
  3. 请求随机间隔,增加 --random-wait 参数,将以wait指定秒数0.5-1.5倍随机等待:
  4.  递归下载 -r ,一般还要同时指定--domains, --exclude-domains, --level 等限制下载内容
  5. 同时下载http页面中的图片等元素--page-requisites 缩写为-p
  6. 支持断点续传 -c
  7. 维持请求间的cookie参数 --save-cookies={saved_file_name} ,如需要同时追踪session的cookie,则增加 --keep-session-cookies,如需要加载以前wget保存的cookie,可使用--load-cookies={saved_file_name}
  8. 指定自定义的http头 --header={header-line},多个头则多次指定该参数
  9. 设置网络超时 --timeout={秒数}-T,设置后 --dns-timeout, --connect-timeout, --read-timeout 也将一起设置,当然也可以另外为它们单独设置。

设置代理

wget不支持socks代理,支持http,https,ftp三类代理,分别为https_prox, /http_proxy, ftp_proxy,下面示例只以http_proxy为例。为wget指定代理有三种方法,推荐第三种

  • 设置环境变量 export http_proxy=http://127.0.0.1:8080
  • 配置文件 /etc/wgetrc或 ~/.wgetrc
  • 在wget 命令行中通过 -e参数(临时设置环境变量)-e参数本身是执行命令,与wgetrc中的设置效果相同。示例:-e "http_proxy=http://127.0.0.1:8080"

更多参考

修订于 2019/12/09

--------- 后面是以前的2013年以前的版本,没啥用,不用看了 ------

需要采集一批数据,是一网站上的产品信息,产品本身信息很简单,使用火车采集器轻松实现;但产品价格数据是ajax响应数据,使用firebug + live http headers抓到该请求,并测试重发,发现该ajax请求必须带一个http头“X-Requested-With: XMLHttpRequest”,没有就返回404错误。

gnu/linux下有大量的杀手级工具,这种情况,要动用其中的wget. wget有很多参数,这里需要使用其中两个:-O,--header. 其中:

-O, 把请求结果保存到指定文件,但我们要使用的却是一个特殊的值:“-”,它可以将结果输出到标准输出

--header, 结http请求加上自定义的头(http header)

于是对于每条产品数据,都发送这样的请求:

wget --header="X-Requested-With: XMLHttpRequest" -O save_123.txt "http://www.website.com/request/path/?Id=123"

这样每条产品都保存到save_123.txt,save_124.txt,save_125.txt....这样的一系列文件中。不过有个问题是,每条数据一个文件,后期分析不方便。所以将其存储到单一的文件里。

方案:先将wget结果指向到标准输出,然后再使用重定向符 >> 定向到单一的文件里。

不过,还有一个问题,每个http响应里没有产品id号,数据跟采集到的产品信息对不上号。于是在每写一笔采集结果前,往文件里先写入该产品的id号。

这里使用echo 命令,要用到echo 的参数:-e, 作用是使用转义符输出换行。示例:echo -e "aaa\nbbb"

于是构造这样的bash脚本:

echo -e "\n"10011"\t"  >>data.log ;wget --header="X-Requested-With: XMLHttpRequest" -O- "http://www.website.com/request/path/?Id=10011" >>data.log; 
echo -e "\n"10012"\t"  >>data.log ;wget --header="X-Requested-With: XMLHttpRequest" -O- "http://www.website.com/request/path/?Id=10012" >>data.log; 
echo -e "\n"10013"\t"  >>data.log ;wget --header="X-Requested-With: XMLHttpRequest" -O- "http://www.website.com/request/path/?Id=10013" >>data.log; sheep 1;
echo -e "\n"10014"\t"  >>data.log ;wget --header="X-Requested-With: XMLHttpRequest" -O- "http://www.website.com/request/path/?Id=10014" >>data.log; 
echo -e "\n"10015"\t"  >>data.log ;wget --header="X-Requested-With: XMLHttpRequest" -O- "http://www.website.com/request/path/?Id=10015" >>data.log; 
echo -e "\n"10016"\t"  >>data.log ;wget --header="X-Requested-With: XMLHttpRequest" -O- "http://www.website.com/request/path/?Id=10016" >>data.log; sheep 1;

第三行加一个sheep 1, 即每采三条暂停1秒钟,作用是防止请求过于密集而影响服务器响应,暂停秒数按实际情况设置。

所有数据都保存到data.log文件里,里面格式如下

10011    
{"data":.....}
10012    
{"data":.....}
10013    
{"data":.....}
10014    
{"data":.....}

每两行一笔数据,其中第一行是产品id号,第二行是采集到的数据。这样就可以方便的提取其中数据了。

其实,对于而需要提取的数据,完全可以通过grep从定向到标准输出的wget结果里提取。只是这里不么做了。

ms sql server 查看服务器当前的连接会话及会话信息(客户端ip地址,协议,时间,读写情况等)

ms sql server 查看服务器当前的连接会话及会话信息(客户端ip地址,协议,时间,读写情况等)

select * from sys.dm_exec_connections

这实际上是master数据库里的一个系统视图,读出来后有不少字段,如client_net_address, protoco_type, connect_time, num_read, num_write等,很明白,就不多说了。

btw,master数据库里有很多系统视图,如:dm_exec_sessions, dm_fts_memory_pools, dm_os_hosts, dm_os_memory_allocations, dm_os_memory_objects, dm_os_threads, sql_logins, types等,很有参考意义

一个使用tar实现对目录作增量备份的bash脚本

一个使用tar实现对目录作增量备份的bash脚本

#!/bin/bash
#program:
#  auto backup the folder $to_backup
#        to $save_to
#  History:
# 2012/11/28  yqf First release

# do NOT change the variable $filename
#  except you REALY know what you are doing exactly
filename=`date +%Y%m%d_%H%M%S`

# sample
# ##whitch folder you want to backup, full path (start with "/")
#to_backup="/var/www/html/foo"
# ##where you whant to save the backup files,folder, MUST end by "/"
#save_to="/var/www/backup/foo/"
# ##the incremental file, file name, full path
#incremental_file="/var/www/backup/incremental_file/foo.incre"
#
#tar -g $incremental_file \
# ## each files or folders that no need to backup, (ingore),
# ##    [Careful] without end slash even folder
#  -C $to_backup \
#  --exclude data/cache \
#  --exclude data/tmp \
#  -cpjf $save_to$filename.tar.bz2 .
# sample end
#
# mysite.com
to_backup="/var/www/html/mysite/html/"
save_to="/home/myhome/backup/mysite/"
incremental_file="/home/myhome/backup/incremenntal_list/mysite.increment_file"

tar -g $incremental_file \
  -C $to_backup \
  --exclude data/static_cache \
  --exclude data/templates_c \
  --exclude data/templates_cache \
  --exclude data/cache \
  -cpjf $save_to$filename.tar.bz2 .

下载脚本附件 tar_incremental_backup_sample
其中....似乎很简单,不用说什么了

grub-install安装grub时出错:/boot/grub/stage1 not read correctly.

在U盘上安装grub,之前是fat32的,格式化成exfat再安装grub,U盘设置文件为/dev/sdc4, 挂载于/media/c0f80392-62db-4156-a4f5-c64fc7515795/,但在运行grub-install时却出错了,如下:

[root@fsc feng]# grub-install --root-directory=/media/c0f80392-62db-4156-a4f5-c64fc7515795/ /dev/sdc4
The file /media/c0f80392-62db-4156-a4f5-c64fc7515795//boot/grub/stage1 not read correctly.

印象中以前都是这样安装grub的,检查没发现问题,于是google,说是/etc/fstab 里作过配置,刚才确实改过但没有重启机器,于是重启。再运行grub-install,还是照例出错,消息完全一样。于是继续google,啃英文文章,一时间没有找到方案。又尝试了N多其它方式:再次格式化U盘、直接拷/boot/grub 到U盘再grub-install等,都不行。

准备把U盘上分区重新删掉,重建分区再尝试。突然想到分区格式与分区表中不一致,使用“磁盘实用工具”查看时,该分区/dev/sdc4是w95 fat32(0x0b),删掉,再建ext2分区,设置为可启动(可以通过磁盘实用工具设置,或fdisk修改都可以),为/dev/sdc1,格式化为ext2,再次运行grub-install,成功

[root@fsc feng]# grub-install --root-directory=/media/287801d6-91aa-4563-a45e-d34073af2ff9 /dev/sdc1
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /media/287801d6-91aa-4563-a45e-d34073af2ff9/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(fd0)    /dev/fd0
(hd0)    /dev/sda
(hd1)    /dev/sdb
(hd2)    /dev/sdc
[root@fsc feng]#