常用数据库mysql,pgsql,mssql,access
Sep
25
考虑到数据优化,现将千万级数据作分表存储便与查询
sql如何查找出number中第一位为1的数据:
标准写法:
select substring(cast(adv_id as varchar) from 1 for 1) as a1 from zhoz_sub_dat_6_3;
常规写法:
select substring(cast(adv_id as varchar), 1,1) as a1 from zhoz_sub_dat_6_3;
INSERT INTO zhoz_sub_dat_6_3
sql如何查找出number中第一位为1的数据:
标准写法:
select substring(cast(adv_id as varchar) from 1 for 1) as a1 from zhoz_sub_dat_6_3;
常规写法:
select substring(cast(adv_id as varchar), 1,1) as a1 from zhoz_sub_dat_6_3;
INSERT INTO zhoz_sub_dat_6_3
May
7
公司老服务器,空间不大。下午哥们说SVN提交不了,原因是磁盘空间不足。
我就简单查询了一下:
[root@oyasai /]# du -h --max-depth=1 |sort
6.2G /usr
19G /var
继续查看,定位到了mysql目录:
-rw-rw----. 1 mysql mysql 3844079616 May 7 11:00 ibdata1
-rw-rw---- 1 mysql mysql 371028567 May 2 13:10 slow_query.log
原来是索引文件和(一堆)日志文件引起的。
1、彻底不需要:
关闭mysql主从,关闭binlog
/etc/mysql/my.cnf。把里面的log-bin这一行注释掉。
# binlog_format=mixed
然后重启数据库
2、重启mysql,开启mysql主从,设置expire_logs_days
我就简单查询了一下:
[root@oyasai /]# du -h --max-depth=1 |sort
6.2G /usr
19G /var
继续查看,定位到了mysql目录:
-rw-rw----. 1 mysql mysql 3844079616 May 7 11:00 ibdata1
-rw-rw---- 1 mysql mysql 371028567 May 2 13:10 slow_query.log
原来是索引文件和(一堆)日志文件引起的。
1、彻底不需要:
关闭mysql主从,关闭binlog
/etc/mysql/my.cnf。把里面的log-bin这一行注释掉。
# binlog_format=mixed
然后重启数据库
2、重启mysql,开启mysql主从,设置expire_logs_days
Mar
19
当执行一条时间比较长的Sql文时,会引起DB挂起。
①查询Pg的状态方法:
# ps axwf | grep 'postgres'
20152 ? S 34:54 /usr/local/pgsql/bin/postgres
20154 ? Ss 54:20 \_ postgres: writer process
20155 ? Ss 41:50 \_ postgres: wal writer process
20156 ? Ss 192:31 \_ postgres: autovacuum launcher process
20157 ? Ss 465:52 \_ postgres: stats collector process
8493 ? Ss 0:00 \_ postgres: postgres WildCard ::1(42985) SELECT
8495 pts/3 S+ 0:00 | \_ grep postgres
②table lock waiting, select waiting产生测试方法,postgres select 执行延迟:
select pg_sleep(50),(select id from zhoz_table) as zhoz_id;
③解决方法:
lock部分mode变换
①查询Pg的状态方法:
# ps axwf | grep 'postgres'
20152 ? S 34:54 /usr/local/pgsql/bin/postgres
20154 ? Ss 54:20 \_ postgres: writer process
20155 ? Ss 41:50 \_ postgres: wal writer process
20156 ? Ss 192:31 \_ postgres: autovacuum launcher process
20157 ? Ss 465:52 \_ postgres: stats collector process
8493 ? Ss 0:00 \_ postgres: postgres WildCard ::1(42985) SELECT
8495 pts/3 S+ 0:00 | \_ grep postgres
②table lock waiting, select waiting产生测试方法,postgres select 执行延迟:
select pg_sleep(50),(select id from zhoz_table) as zhoz_id;
③解决方法:
lock部分mode变换
Feb
20
define("CRITERIA_TOTAL_TYPE_ARRAY", "OVER_18, FEMALE_28");
根据此条件取出所有年龄超过18,并且如果性别是FEMALE>28的数据。
$criteria_sql = "";
if ($criteria_flg) {
$user_sql = "";
// OVER_18, FEMALE_28
$param_list = explode(",", CRITERIA_TOTAL_TYPE_ARRAY);
$over_age = explode("_", $param_list[0]);
if (isset($over_age[1])) {
$user_sql .= " and date_trunc('day', birthday) < '" . date("Y-m-d", strtotime("-" . $over_age[1] . " year")) . "'";
}
if (isset($param_list[1])) {
$sex_age = explode("_", $param_list[1]);
$sex = trim($sex_age[0]);
$user_sql .= " and (sex !='" . $sex . "' or (sex='" . $sex . "' and date_trunc('day', birthday) < '" . date("Y-m-d", strtotime("-" . $sex_age[1] . " year")) . "'))";
}
if (!empty($user_sql)) {
$criteria_sql = " inner join (select * from user_mst where delete_flg=false " . $user_sql . ") as u on (u.id=pad.user_id)";
}
}
根据此条件取出所有年龄超过18,并且如果性别是FEMALE>28的数据。
$criteria_sql = "";
if ($criteria_flg) {
$user_sql = "";
// OVER_18, FEMALE_28
$param_list = explode(",", CRITERIA_TOTAL_TYPE_ARRAY);
$over_age = explode("_", $param_list[0]);
if (isset($over_age[1])) {
$user_sql .= " and date_trunc('day', birthday) < '" . date("Y-m-d", strtotime("-" . $over_age[1] . " year")) . "'";
}
if (isset($param_list[1])) {
$sex_age = explode("_", $param_list[1]);
$sex = trim($sex_age[0]);
$user_sql .= " and (sex !='" . $sex . "' or (sex='" . $sex . "' and date_trunc('day', birthday) < '" . date("Y-m-d", strtotime("-" . $sex_age[1] . " year")) . "'))";
}
if (!empty($user_sql)) {
$criteria_sql = " inner join (select * from user_mst where delete_flg=false " . $user_sql . ") as u on (u.id=pad.user_id)";
}
}
May
17
以前一个表中的字段忘记记录了,想修复数据。可以用一个Sql文的Update来实现。
方法有两种:
update adv_dat set adv_type=adv_mst.adv_type, adv_target=adv_mst.adv_target
from adv_mst where adv_media_total_day_dat.adv_id=adv_mst.id;
另外一种就是用 left join来实现
方法有两种:
update adv_dat set adv_type=adv_mst.adv_type, adv_target=adv_mst.adv_target
from adv_mst where adv_media_total_day_dat.adv_id=adv_mst.id;
另外一种就是用 left join来实现