我如何查询一个被分成 2 个较小表的表?联盟?看法?

发布于 2024-09-04 15:51:53 字数 489 浏览 5 评论 0原文

我有一个非常大的表(近 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯角 2024-09-11 15:51:53

如果有理由不这样做,您应该合并这些表,而不是不断查询这两个表。
StackOverflow 上有一个关于这样做的问题:

如何合并两个 MySQL 表?< /a>

如果需要将它们分开,可以使用以下语法:

SELECT table1.column1, table2.column2 FROM table1, table2 WHERE table1.column1 = table2.column1;

这里有一篇关于何时使用 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:

SELECT table1.column1, table2.column2 FROM table1, table2 WHERE table1.column1 = table2.column1;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文