zoukankan      html  css  js  c++  java
  • select判断+insert插入的原子操作 pgsql

    使用insert into tablA select * from tableB语句时,

    一定要确保tableB后面的where,order或者其他条件,都需要有对应的索引,

    来避免出现tableB全部记录被锁定的情况。

    通过sql一步处理

    insert into table_name(column1,column2,column3) 
    select 'value1','value2','value3'
    where (select column from table_name where id=1) is null;

    eg:

    insert into subscribe_member(openid,msg_title,can_times)
    select 'hhhh','biaoti',1
    where (select openid from subscribe_member where id=1) is null;

    使用insert into tablA select * from tableB语句时,一定要确保tableB后面的where,order或者其他条件,都需要有对应的索引,来避免出现tableB全部记录被锁定的情况。

    https://mp.weixin.qq.com/s/B5WkFW0RkR7HgbyUdd7BFw

    最终的sql:

    INSERT INTO order_record SELECT
        * 
    FROM
        order_today FORCE INDEX (idx_pay_suc_time)
    WHERE
        pay_success_time <= '2020-03-08 00:00:00';

    补充:

    force index() 指令可以指定本次查询使用哪个索引!一条sql只会用到一个索引,mysql优化器会计算出一个合适的索引,但是这个索引不一定是最好的。force index()指令可以避免MySql优化器用到了一个低效的索引。

    mysql可能并不总会选择合适且效率高的索引去查询,这时适当的force index(indexname) 强制告诉mysql使用什么索引尤为重要。

  • 相关阅读:
    mysql优化之索引优化
    mysqld --debug-sync
    mysql.cnf 配制文件详解
    my.cnf 中字符集设置
    tcp_tw_reuse 与 net.ipv4.tcp_tw_recycle
    mysql init_connect 参数的其他用处
    监控mysql索引使用效率的脚本
    mysql 源代码学习 博客 [lock..]
    mysqld with valgrind
    思维导图软件
  • 原文地址:https://www.cnblogs.com/mjbenkyo/p/11990809.html
Copyright © 2011-2022 走看看