MySQL数据库汇总

SHOW STATUS LIKE '%Connection%';

SHOW VARIABLES LIKE '%max_connections%';

SHOW PROCESSLIST;

 

show status;

+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_cached    | 58    |
| Threads_connected | 57    |   ###这个数值指的是打开的连接数
| Threads_created   | 3676  |
| Threads_running   | 4     |   ###这个数值指的是激活的连接数,这个数值一般远低于connected数值

 

数据库性能优化

1.建立索引

2.多表关联进行单表查询

3.数据查询where条件进行从右先过滤大的条件

5、数据库主从分离,读写分离,降低读写针对同一表同时的压力,至于主从同步,MySQL有自带的binlog实现 主从同步;

 

数据处理

INSERT INTO city_config(city_id,city_name,city_code,city_status) SELECT cityid,city,cityid,1 FROM t_cities where city='乌鲁木齐市'
 

SELECT
count(1)
FROM
hzc.usr_profiles as u
LEFT JOIN hzc.tblorder AS t ON u.id=t.profile_id
where
u.mobile in (SELECT
a.mobile
FROM
opt.opt_user_travel_info as a) AND t.OrderID IS NULL or t.payment_status in(3,4);

 
--         SELECT  count(TIMESTAMPDIFF(HOUR,now(),DATE_ADD(election_time, INTERVAL 15 DAY)))  as  hours
-- from apply_city_master
select count(id) from apply_city_master where HOUR(timediff(now(), expiry_time)) <= 24 and apply_status=2

SELECT order_num,SUM(quantity*item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price) >=50 ORDER BY ordertotal;

SELECT * FROM hzc.tblorder where contact_mobile in (SELECT a.mobile FROM opt.opt_user_travel_info as a limit 0,13369)
select * from hzc.vas_cdl_order_details where (last_ename='test' or first_ename='hzc') order by CreateDtTm desc
 

非前导则可以使用索引

select name from user where name like 'zhangsan%'

SELECT
a.order_no
FROM
hzc_tmp_201807201934.vas_orders a
WHERE
a.vas_order_type = 'C'
AND a.order_date BETWEEN '2018-07-20' AND '2018-07-21'
AND a.order_status = 1
AND a.payment_status IN (0 , 2)
AND NOT EXISTS( SELECT
1
FROM
hzc.vas_orders
WHERE
order_no = a.order_no)
 
   
 
数据库引擎
MySQL中myisam与innodb的区别,至少5点
InnoDB是基于索引来完成行锁
 
  • 1>.InnoDB支持事物,而MyISAM不支持事物
  • 2>.InnoDB支持行级锁,而MyISAM支持表级锁
  • 3>.InnoDB支持MVCC, 而MyISAM不支持
  • 4>.InnoDB支持外键,而MyISAM不支持
  • 5>.InnoDB不支持全文索引,而MyISAM支持。
  1. from (注:这里也包括from中的子语句)
  2. join
  3. on
  4. where
  5. group by(开始使用select中的别名,后面的语句中都可以使用)
  6. avg,sum.... 等聚合函数
  7. having
  8. select
  9. distinct
  10. order by
  11. limit

 

1. 怎么验证 mysql 的索引是否满足需求?

使用 explain 查看 SQL 是如何执行查询语句的,从而分析你的索引是否满足需求。

 explain 语法:explain select * from table where type=1。

 

SELECT t1.id,t1.city_id,t1.city_name,t1.create_time,
GROUP_CONCAT(t2.mobile)as mobile,

TIMESTAMPDIFF(DAY,now(),min(t2.expiry_time)),

GROUP_CONCAT(t2.expiry_time)as mobile,
min(t2.expiry_time) as mindate,
t1.city_status,
t1.is_citymaster,
(
CASE
WHEN (TIMESTAMPDIFF(DAY,now(),min(t2.expiry_time))>=10) THEN 10
WHEN (t1.is_citymaster=1 &&t1.is_citydeputy1=1 && t1.is_citydeputy2=1) THEN 0
ELSE 1
END
)
AS masterStatus
from city_config as t1
left join apply_city_master as t2
on t1.id=t2.city_id
where t2.apply_status=2
GROUP BY t1.city_id
ORDER BY t2.city_type
 
select * from income_details
where user_id=1067
and city_id=250
and city_type=2
and apply_status=1
and create_time>='2019-07-22 23:00:00'
and create_time<='2019-08-06 23:00:00'
 
:="INSERT INTO student(id,name,age) VALUES("&A2&",'"&B2&"','"&C2&"')" 
 
 
SELECT `userspk`.`avatar` AS `user_avatar`, `a`.`user_id`, `a`.`answer_record`, MAX(`score`) AS `score` FROM (select * from pkrecord order by score desc) as a INNER JOIN `userspk` AS `userspk` ON `a`.`user_id` = `userspk`.`user_id` WHERE `a`.`status` = 1 AND `a`.`user_id` != 'm_6da5d9e0-4629-11e9-b5f7-694ced396953' GROUP BY `user_id` ORDER BY `a`.`score` DESC LIMIT 9;
 
//不使用子查询
SELECT `userspk`.`avatar` AS `user_avatar`, `pkrecord`.`user_id`, `pkrecord`.`answer_record`, `pkrecord`.`id`, MAX(`score`) AS `score` FROM pkrecord INNER JOIN `userspk` AS `userspk` ON `pkrecord`.`user_id` = `userspk`.`user_id` WHERE `pkrecord`.`status` = 1 AND `pkrecord`.`user_id` != 'm_6da5d9e0-4629-11e9-b5f7-694ced396953' GROUP BY `user_id` ORDER BY `pkrecord`.`score` DESC LIMIT 9;
 
="insert into hzc_activity.app_layer_country_default_city(country,city_id) values ('"&A1&"',"&B1&");"


ALTER TABLE `hzc`.`mkt_coupon_policies`
ADD COLUMN `isRemind` BIT(1) NULL DEFAULT b'0' AFTER `coupon_policy_car_group_id`;
 
ALTER TABLE `hzc`.`ref_drv_coupon_label`
ADD COLUMN `channel` VARCHAR(200) NULL DEFAULT NULL AFTER `country_id`;
 

MySql查询某一天的数据

select SUM(actual_deduction_price) from income_details where user_id=20 and create_time>='2019-07-25 00:00:00' and create_time<='2019-07-25 23:59:59';


select SUM(actual_deduction_price) from income_details where user_id=20 and create_time between '2019-07-25 00:00:00' and '2019-07-25 23:59:59';


SELECT SUM(actual_deduction_price) from income_details where user_id=20 and create_time > '2019-07-25' and create_time < '2019-07-26';


SELECT SUM(actual_deduction_price) from income_details where user_id=20 and (datediff(create_time,'2019-07-25')=0);

 

 
create sequence seq_USERS_id start with 1 increment by 1;
 
insert into users values (seq_USERS_id.nextval,'张三','1','2');
 

 
select * from user where name in (select name from user group by name having count(1) > 1);
 
SELECT * FROM user AS a WHERE EXISTS ( SELECT 1 FROM user AS b WHERE a.name = b.name GROUP BY name HAVING COUNT(1) > 1 )

 

SELECT IFNULL(l1.wechat_nick_name, l2.mobile) AS wechatName, l1.wechat_head_img_url AS wechatImg FROM hzc.acy_oldwithnew_bundling t1 left JOIN hzc.acy_users l1 ON t1.to_profile_no = l1.profile_no left JOIN hzc.usr_profiles l
 
select * from user where id in( select min(id) from user where name = 'Java3y' and pv = 20 and time='7-25' group by name,pv,time; )
 
select name,time,sum(pv) as pv from xxx_table where name = 'Java3y' group by name,time
 
 
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1
7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1
 
select supplier,count(id) from xxx_table where sendtime >= timestamp '2019-06-01'
 
MySQL数据库汇总_第1张图片

 

  MySQL数据库汇总_第2张图片

 

索引:B+,B-,全文索引
Mysql的索引是一个数据结构,旨在使数据库高效的查找数据。
常用的数据结构是B+Tree,每个叶子节点不但存放了索引键的相关信息还增加了指向相邻叶子节点的指针,这样就形成了带有顺序访问指针的B+Tree,做这个优化的目的是提高不同区间访问的性能。
什么时候使用索引:

经常出现在group by,order by和distinc关键字后面的字段

经常与其他表进行连接的表,在连接字段上应该建立索引

经常出现在Where子句中的字段

经常出现用作查询选择的字段

1)首先我们可以用EXPLAIN或DESCRIBE(简写:DESC)命令分析一条查询语句的执行信息。

DESC SELECT * FROM `user`

2)优化子查询

在MySQL中,尽量使用JOIN来代替子查询。因为子查询需要嵌套查询,嵌套查询时会建立一张临时表,临时表的建立和删除都会有较大的系统开销,而连接查询不会创建临时表,因此效率比嵌套子查询高。

3)使用索引

索引是提高数据库查询速度最重要的方法之一,使用索引的三大注意事项包括:

LIKE关键字匹配'%'开头的字符串,不会使用索引;

OR关键字的两个字段必须都是用了索引,该查询才会使用索引;

使用多列索引必须满足最左匹配。

4)分解表

对于字段较多的表,如果某些字段使用频率较低,此时应当将其分离出来从而形成新的表。

5)中间表

对于将大量连接查询的表可以创建中间表,从而减少在查询时造成的连接耗时。

6)增加冗余字段

类似于创建中间表,增加冗余也是为了减少连接查询。

7)分析表、检查表、优化表

分析表主要是分析表中关键字的分布;检查表主要是检查表中是否存在错误;优化表主要是消除删除或更新造成的表空间浪费。

分析表: 使用 ANALYZE 关键字,如ANALYZE TABLE user

 

select t1.id,t1.city_name,GROUP_CONCAT(t2.mobile) as mobile,t2.city_type,t2.apply_status
from city_config as t1
left join apply_city_master as t2
on t1.id=t2.city_id
where t2.apply_status=4
and t1.id=363
GROUP BY city_type
ORDER BY t2.create_time

 

 

select  GROUP_CONCAT(t2.mobile) as mobile,t2.city_type from  apply_city_master as t2 where  t2.city_id=363  and  t2.apply_status=4  GROUP BY t2.city_type ORDER BY t2.create_time


select

(
CASE
WHEN (a1.city_type=1) THEN a1.mobile
END
)AS citymaster,

 

(
CASE
WHEN (a1.city_type=2) THEN a1.mobile
END
)AS citymaster2,

(
CASE
WHEN (a1.city_type=3) THEN a1.mobile
END
)AS citymaster3

from
(
select GROUP_CONCAT(t2.mobile) as mobile,t2.city_type from apply_city_master as t2 where t2.city_id=363 and t2.apply_status=4 GROUP BY t2.city_type ORDER BY t2.create_time
) as a1

 


select
(
CASE
WHEN (a1.city_type=1) THEN a1.mobile
WHEN (a1.city_type=2) THEN a1.mobile
WHEN (a1.city_type=3) THEN a1.mobile
END
)AS citymaster
from
(
select GROUP_CONCAT(t2.mobile) as mobile,t2.city_type from apply_city_master as t2 where t2.city_id=363 and t2.apply_status=4 GROUP BY t2.city_type ORDER BY t2.create_time
) as a1

 

转载于:https://www.cnblogs.com/ywsheng/p/11236576.html

你可能感兴趣的:(MySQL数据库汇总)