×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

SQL tunning,

1. database: MySQL

2. table_1 includes 5000 records.

table_1_id, create_date, status

3. table_2 includes 20 million records

table_2_id, table_1_id, user_id

4. 我的问题是要 找出记录table_1 , 中,
SELECT * FROM table_1 WHERE CREATE_DATE > SOMEDATE AND STATUS =' 2'
并且, table_2 不包含 table_1_id 且 user_id <>'abc'. 怎么做好, 不太想用兵NOT IN

5. 我是这么做的
a. rs1= SELECT * FROM table_1 WHERE CREATE_DATE > SOMEDATE AND STATUS =' 2'
b. LOOp result from a
for example. like
while (rs1 ...) {
rs2 = select * from table_2 where table_1_id= result1.table_1_id and name='abc'
if (rs2..) {
print '不是需要的'
} else {
//print 找到记录
}

}

6. 是不是,有更好的办法, 多谢了
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / SQL tunning,
    1. database: MySQL

    2. table_1 includes 5000 records.

    table_1_id, create_date, status

    3. table_2 includes 20 million records

    table_2_id, table_1_id, user_id

    4. 我的问题是要 找出记录table_1 , 中,
    SELECT * FROM table_1 WHERE CREATE_DATE > SOMEDATE AND STATUS =' 2'
    并且, table_2 不包含 table_1_id 且 user_id <>'abc'. 怎么做好, 不太想用兵NOT IN

    5. 我是这么做的
    a. rs1= SELECT * FROM table_1 WHERE CREATE_DATE > SOMEDATE AND STATUS =' 2'
    b. LOOp result from a
    for example. like
    while (rs1 ...) {
    rs2 = select * from table_2 where table_1_id= result1.table_1_id and name='abc'
    if (rs2..) {
    print '不是需要的'
    } else {
    //print 找到记录
    }

    }

    6. 是不是,有更好的办法, 多谢了
    • 用 not exists
      select * from table_1 t1 where not exists (select 1 from table_2 t2 where t2.table_1_id = t1.table_1_id and t2.user_id<>'xxx')

      最好在 table_2.table_1_id 上建索引