Maison  >  Questions et réponses  >  le corps du texte

mysql - 如何高效的查询需要合并大数据表的操作

1.存在两个数据库Shanghai和Beijing(假设其他数据丢失,中国人口数据只剩下上海和北京)
2.两个数据库中都有同样结构的表table_people,数据量都在一千万左右
3.有以下数据
Shanghai.table_people

id username birthday
1 jhon 2016-08-15 12:00:00
2 may 2016-08-14 13:00:00
3 abcd1111 2015-08-01 14:00:00
... .... .....

Beijing.table_people

id username birthday
1 mike 2016-08-16 23:58:00
2 kitty 2016-08-03 15:00:06
3 ab111 2014-01-01 15:16:18
... .... .....

问:
1.如何高效的(1s内)查出最近在中国出生的前十个人
2.对于YII框架数据提供器应该怎么写

天蓬老师天蓬老师2742 Il y a quelques jours988

répondre à tous(4)je répondrai

  • PHP中文网

    PHP中文网2017-04-17 15:26:04

    (SELECT * FROM Beijing.table_people ORDER BY birthday limit 10)
    union all
    (SELECT * FROM shanghai.table_people ORDER BY birthday limit 10)
    ORDER BY birthday limit 10
    

    你看这种思路可以吗?

    répondre
    0
  • 迷茫

    迷茫2017-04-17 15:26:04

    birthday类型得是datetime不能是string,然后作索引。查的时候先union all再order by再limit 10。mysql内部应该是就是对两个索引做一下归并排序

    如果想更快,就把birthday类型改成int的

    répondre
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:26:04

    每个表都取符合条件的前10条数据,然后再对取出来的数据在代码里排序并取出最终的前10条数据。

    不过这样写代码也很累,以后加一个表就得改代码,所以建议找一下数据库中间件的相关资料。

    répondre
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:26:04

    两个sql,根据id desc 以及limit 10,都插入到一个临时表,对20条数据时间排序再limit10

    répondre
    0
  • Annulerrépondre