我如何查询一个被分成 2 个较小表的表?联盟?看法?
我有一个非常大的表(近 2,000,000 条记录),它被分成 2 个较小的表。一个表仅包含上周的记录,另一个表包含所有其余的记录(很多......)
现在我得到了一些存储过程/函数,用于在分割之前查询大表。
我仍然需要它们来查询两个表的并集,但是似乎创建一个使用两个表之间的并集语句的视图会永远持续下去......
这就是我的观点:
CREATE VIEW `united_tables_view` AS select * from table1 union select * from table2;
然后我想在存储过程中切换 select从“oldBigTable”到从“united_tables_view”中选择...
我尝试添加索引来缩短时间,但没有任何帮助... 有什么想法吗?
PS:
视图和联合是我的想法,但任何其他创意都是完美的!
来吧!
谢谢!
I have a very big table (nearly 2,000,000 records) that got split to 2 smaller tables. one table contains only records from last week and the other contains all the rest (which is a lot...)
now i got some Stored Procedures / Functions that used to query the big table before it got split.
i still need them to query the union of both tables, however it seems that creating a View which uses the union statement between the two tables lasts forever...
that's my view:
CREATE VIEW `united_tables_view` AS select * from table1 union select * from table2;
and then i'd like to switch everywhere the Stored procedure select from 'oldBigTable' to select from 'united_tables_view'...
i've tried adding indexes to make the time shorter but nothing helps...
any Ideas?
PS
the view and union are my idea but any other creative idea would be perfect!
bring it on!
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有理由不这样做,您应该合并这些表,而不是不断查询这两个表。
StackOverflow 上有一个关于这样做的问题:
如何合并两个 MySQL 表?< /a>
如果需要将它们分开,可以使用以下语法:
这里有一篇关于何时使用 SELECT、JOIN 和 UNION 的文章
https:// web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-1050307.html
If there is a reason not to, you should merge the tables rather than constantly query both of them.
Here is question on StackOverflow about doing that:
How can I merge two MySQL tables?
If you need to keep them seperate, you can use syntax along the lines of:
Here is an article about when to use SELECT, JOIN and UNION
https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-1050307.html