简体   繁体   English

MySQL - 跨越两个表的最佳实践

[英]MySQL - Best practice to cross two tables

I have two tables. 我有两张桌子。 Each one is populated with about 50K records. 每个人都有大约50K记录。 Both have one common field. 两者都有一个共同的领域。 I want the records of both tables where the common field is matching. 我想要公共字段匹配的两个表的记录。 So I use the following SQL code. 所以我使用以下SQL代码。 The problem is that the query is running now for already 20 minutes and nothing happening. 问题是查询现在已经运行了20分钟而且什么也没发生。 It seems strange to me that it is taking so much time. 对我来说,这花了很多时间似乎很奇怪。 I wonder if there is a better way to achieve my target. 我想知道是否有更好的方法来实现我的目标。 Thank you in advance for your replies. 提前感谢您的回复。

My SQL code: 我的SQL代码:

Select * from tableOne T1, tableTwo T2 
Where T1.name = T2.name

By the way I am open to php solutions (loops or whatever if it is better...) 顺便说一句,我对php解决方案开放(循环或其他任何东西,如果它更好......)

Two things: 两件事情:

  1. You need to make sure there is an index on the name column in both tables. 您需要确保两个表中的name列都有索引。 Otherwise the query will have to essentially scan every row of each table looking for matches. 否则,查询将必须扫描每个表的每一行以查找匹配项。

  2. Ideally, the index should have "included columns" that you want to select in the query. 理想情况下,索引应包含要在查询中选择的“包含列”。 Otherwise the query won't be able to fully use the index, but will have to go back to the table and pick up the other columns manually for each row. 否则查询将无法完全使用索引,但必须返回到表并为每行手动获取其他列。 This means you should consider selecting only the columns you need (rather than *) in the query, and adding those columns as included columns to the index. 这意味着您应该考虑在查询中仅选择所需的列(而不是*),并将这些列作为包含的列添加到索引中。

Try this: 尝试这个:

select *
from tableOne t1
inner join tableTwo t2 on t1.name = t2.name

Althought I doubt that would make a difference, trying won't hurt. 虽然我怀疑这会有所作为,尝试不会受到伤害。 It's not recommended to use from tableOne t1, tableTwo t2 , because you are implicitly making a cartesian product! 不建议使用from tableOne t1, tableTwo t2 ,因为你隐式制作了一个笛卡尔积!

I believe it's better to use Join 我相信最好使用Join

Something like this: 像这样的东西:

SELECT *
FROM tableOne
INNER JOIN TableTwo
ON tableOne.name=TableTwo.name

应该是一个内部联接,以从两个表中获取名称连接的行。

SELECT t1.*, t2.* FROM table1 t1 INNER JOIN table2 t2 ON t1.name = t2.name

MySQL Inner Joins - what you have happening there - are very efficient. MySQL内部联接 - 你在那里发生的事情 - 非常有效。 50K records is not very much at all, and twenty minutes is ridiculously long even for a pair of tables with a couple million entries. 50K的记录根本不是很多,即使是一对有几百万个条目的表,也只有20分钟。 Try to restart the query, or just restart the whole web server. 尝试重新启动查询,或只是重新启动整个Web服务器。 The query itself if fine (although it's good practice to specify exact which columns from each table you want, even if you want every column except one). 查询本身如果正常(尽管最好指定您想要的每个表中的哪些列,即使您希望除了一列之外的每一列)。 Adding an index if you don't have one is also a good idea. 如果没有索引,添加索引也是一个好主意。

What is your tables' storage engine? 你桌子的存储引擎是什么? If it is InnoDB, you would need to set a foreign key between these two tables -- that will definitely solve your performance problem. 如果它是InnoDB,您需要在这两个表之间设置一个外键 - 这肯定会解决您的性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM