如果对数据精确性要求不高,可以设置读写不互锁

非事务型引擎(Myisam/Aria)

SET GLOBAL concurrent_insert = 2; #NEVER=0,AUTO=1,ALWAYS=2

这个设置完需要定期优化表,否则会造成大量碎片.

查看参数状态: ~~~mysql show variables like ‘concurrent_insert’; ~~~

事务型引擎(Innodb)

SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;

查看参数状态: ~~~mysql SELECT @@global.tx_isolation ~~~

用mysql主从同步做测试环境

既想使用主库数据, 又想随意update/delete/insert. 做一个省心的测试环境 在从库/etc/my.cnf中加入选项: slave_skip_errors = all

Mysiam/Aria性能调优

key Buffer 命中率 mysql>show global status like ‘key%’; key_buffer_read_hits = (1-key_reads / key_read_requests) * 100% key_buffer_write_hits = (1-key_writes / key_write_requests) * 100%

按pattern导出表

mysql development -udevelopment -pdevelopment -e 'show tables like "root_%"' |grep -v Tables_in |xargs mysqldump development -udevelopment -pdevelopment > root.sql

删除重复项

ALTER IGNORE TABLE your_table ADD UNIQUE INDEX `tmp_index` (URL_ADDR);

表之间批量拷贝数据

insert into table2(column3,column4) select column1,column2 from table1 where 1;

设置用户权限

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH   PRIVILEGES;

批量删除列

ALTER TABLE poi drop column name_py,
drop column typename,
drop column typename_py,
drop column provinceid,
drop column provincename,
drop column provincename_py,
drop column cityname,
drop column cityname_py,
drop column districtid,
drop column districtname,
drop column districtname_py,
drop column streetname_py,
drop column address,
drop column address_py,
drop column cid,
drop column showflag,
drop column trip_py,
drop column namelen,
drop column displayorder;

格式化小数

format("%.2f",'123.456')

gem install 加速

gem install gem-fast

数组求平均值

http://stackoverflow.com/questions/1341271/average-from-a-ruby-array

ruby 正则测试

http://rubular.com/

rails activerecord 常用方法

官方手册 http://guides.rubyonrails.org/active_record_querying.html

基本用法

Client.take #SELECT * FROM clients LIMIT 1 Client.first #SELECT * FROM clients ORDER BY clients.id ASC LIMIT 1 Client.last #SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1 Client.find(10) #select * SELECT * FROM clients WHERE (clients.id = 10) LIMIT 1

批量插入

Client.create(array_of_new_rows)
Array.each_slice(50) do |batch|
new_rows = []
batch.each do |url|
seg ,id = URI(url).path.split('||')
new_rows << {:seg=>seg,:id=>id}
end
Client.create(new_rows)
end

debug

require 'logger'
ActiveRecord::Base.logger = Logger.new(STDOUT)

MD5摘要

Digest::MD5.hexdigest("string")

如何创建一个GEM

http://rakeroutes.com/blog/lets-write-a-gem-part-one/

批量插入

activerecord-import

books = []
10.times do |i|
  books << Book.new(:name => "book #{i}")
end
Book.import books

ruby操作SQL Server的注意事项

使用with nolock ~~~ruby class ElongKeywordDetail < ActiveRecord::Base establish_connection $dbconfig[‘web’] self.table_name = “Keyword” scope :lock, -> {‘WITH (NOLOCK)’} end ~~~

tidy_tds可能会遇到character buffer overflow的问题,是由于sqlserver数据库中包含全角字符引起的,需要在sql语句中通过replace替换掉。

ruby使用send灵活调用方法

一般的写法:

ta.rank0 = ranks[0]
ta.rank1 = ranks[1]
ta.rank2 = ranks[2]
ta.rank3 = ranks[3]
ta.rank4 = ranks[4]

灵活的写法:

ranks.each_with_index do |rank,index|
  ta.send(:"rank#{index}=",rank)
end

用\uFFFF的形式打印字符串

string.unpack('U*').map{ |i| "\\u" + i.to_s(16).rjust(4, '0') }.join

在home目录总安装gem

gem install xx.gem --user-install

进制转换

10进制

"%u" % 1000 #1000

16进制

"%x" % 1000 #3e8

Enumerable

# Sum some numbers (5..10).reduce(:+) #=> 45 # Same using a block and inject (5..10).inject {|sum, n| sum + n } #=> 45 # Multiply some numbers (5..10).reduce(1, :*) #=> 151200 # Same using a block (5..10).inject(1) {|product, n| product * n } #=> 151200 # find the longest word longest = %w{ cat sheep bear }.inject do |memo,word| memo.length > word.length ? memo : word end longest #=> “sheep”

db.collection.update({}, {$rename: {‘field1’: ‘field2’}}, false, true); db.collection.renameCollection(‘new_collection’) db.collection.distinct(‘filed’)

db.createCollection(“noautoid”, { autoIndexId: false })

http://stackoverflow.com/questions/12378320/mongodb-inserting-doc-without-id-field

By default, all regular collections automatically insert an _id field if it is absent.

However, this behavior can be changed when you create the collection, by setting explicitely the autoIndexId parameter to false.

> db.createCollection("noautoid", { autoIndexId: false })
{ "ok" : 1 }
Then you can insert documents without _id field. But some drivers, like the javascript one (and so the mongo console), add the _id field by themselves. In the mongo console, you can do this:

> db.noautoid._mongo.insert(db.noautoid._fullName, {name: "Jack"})
> db.noautoid.find()
{ "name" : "Jack" }
More information about the autoIndexId field can be found in the MongoDB documentation. This page is about Capped Collections but the autoIndexId field is common to both regular and capped collections.

#!/bin/sh
#
# usage: retrieve-cert.sh remote.host.name [port]
#
REMHOST=$1
REMPORT=${2:-443}

echo |\
openssl s_client -connect ${REMHOST}:${REMPORT} 2>&1 |\
sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'

使用方法举例 把上面的代码存成foo.sh 执行命令:

sh foo.sh api.baidu.com

http://www.madboa.com/geek/openssl/#cert-retrieve

  • 安装最新版zsh
  • 把bash换成zsh
chsh -s $(which zsh)
  • 安装 oh my zsh
curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh

使用ctrl+r查找历史命令

bindkey "^R" history-incremental-search-backward