mysql数据库sql查询语句:多条件判断

mysql数据库sql查询语句:多条件判断,第1张

1、创建测试表,

create table test_person(id int, RMB int)

2、插入测试数据

insert into test_person values(1,180)

insert into test_person values(2,170)

insert into test_person values(3,290)

insert into test_person values(4,160)

insert into test_person values(5,299)

insert into test_person values(6,266)

insert into test_person values(7,155)

3、查询表中所有记录,select t.* from test_person t,

4、编写sql,汇总每个vip类型的用户数,

select vip_type, count(distinct id)

from (select case when RMB>100 and RMB<200 then 'VIP1' when RMB>200 then 'VIP2' end as vip_type, id

        from test_person) t

group by vip_type

这个应该不会太慢吧,我建议你看一下,你是不是循环做了太多次的插入/更新 *** 作

mysql默认的配置中,每次事务提交都要写binlog和redo log,如果循环太多次——比如循环插入10w条记录——就会非常慢。一般优化思路分两种:

1 修改 sync_binlog为一个100-1000间的值,让binlog每隔100-1000个事务后再写一次;修改innodb_flush_log_at_trx_commit =2; 这么搞的好处是降低了写log的次数和消耗的时间,缺点是,中间出错的话,会丢失一部分的binlog和redolog导致无法通过他们来在出问题是恢复生产库数据。

2 将所有的插入/更新 *** 作放到一个事务中进行。这样,显然就只需要一次写binlong和redolog咯。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8577378.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-18
下一篇2023-04-18

发表评论

登录后才能评论

评论列表(0条)

    保存