JET SQL 性能/ SQL 理解
这不是一个需要回答的问题,更多的是询问为什么会发生这种情况。
我在一个表中有一个字段填充了“Y”或“N”,并且我有一个查询,该查询仅获取该字段的值并弹出到另一个表中
该表大约有 25,000 条记录
下面的查询大约需要运行时间为 25 秒
UPDATE ObjectivesApproved
INNER JOIN Approved
ON ObjectivesApproved.ID = Approved.ID
SET ObjectivesApproved.Football = [Approved].[Cri Football Related];
删除 JOIN 操作会使查询花费更长的时间。
然而,如果我执行下面的操作,整个操作花费的时间不到 5 秒,即使它正在执行 2 个查询,
UPDATE ObjectivesApproved
INNER JOIN Approved
ON ObjectivesApproved.ID = Approved.ID
SET ObjectivesApproved.Football = 'Y'
WHERE (([Approved].[Cri Football Related]='Y'));
UPDATE Approved
INNER JOIN ObjectivesApproved
ON Approved.ID = ObjectivesApproved.ID
SET ObjectivesApproved.Football = 'N'
WHERE (([ObjectivesApproved].[Football] Is Null));
我对我的解决方法很满意,即使它有点不优雅,但为了进一步我对 SQL 的理解,为什么会发生这种情况?
This is not a problem that needs answering, more of a query on why this is happening.
I have a field in one table that is populated with 'Y' Or 'N', and i have a query that simply takes the value of that field and pops into another table
The table has approximately 25,000 records in it
The query below takes about 25 seconds to run
UPDATE ObjectivesApproved
INNER JOIN Approved
ON ObjectivesApproved.ID = Approved.ID
SET ObjectivesApproved.Football = [Approved].[Cri Football Related];
Removing the JOIN operation makes the query take even longer.
If however i do the below, the entire operation takes less than 5 seconds, even though it's executing 2 queries
UPDATE ObjectivesApproved
INNER JOIN Approved
ON ObjectivesApproved.ID = Approved.ID
SET ObjectivesApproved.Football = 'Y'
WHERE (([Approved].[Cri Football Related]='Y'));
UPDATE Approved
INNER JOIN ObjectivesApproved
ON Approved.ID = ObjectivesApproved.ID
SET ObjectivesApproved.Football = 'N'
WHERE (([ObjectivesApproved].[Football] Is Null));
I'm happy with my workaround, even if it is a little inelegant, but to further my understanding of SQL why might this be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论如何,您的第一个版本都会更新 25K 行,但它必须保持表同步,因为它逐行使用从一个到另一个的值。更新的每一行都必须从字段中读取 25K 次。
您的第二个版本(两个语句)过滤数据而不是逐行比较。在内部找到一组记录,然后批量更新,而不是逐行计算。值“Y”不必每次都查找 - 它是常量。
想象一下,如果我要求你根据我给你的清单将 25K 个盒子涂成黑色或白色。拿起第一个盒子,检查列表,并为其着色,拿起第二个盒子,检查列表,为其着色,然后重复,是不是更快?或者是拉出所有应该是白色的并给它们上色,然后拉出所有黑色的并给它们上色是更快吗?请注意,在第二种情况下您只需“检查列表”两次,但在第一种情况下则需要“检查列表”25K 次。
Your first version is updating 25K rows no matter what but it has to keep the tables in sync since it is using values from one into the other on a row by row basis. Every row that is updated has to read from a field - 25K times.
Your second version (both statements) filter data instead of row by row comparison. Internally a set of records is found and then updated in a batch instead of row by row computation. The value 'Y' doesn't have to be looked up each time - it is constant.
Imagine if I asked you to color 25K boxes black or white based on a list I gave you. Is it faster to pick up the first box, check the list, and color it, pick up the second box, check the list, color it, repeat. Or is it faster to pull out all ones supposed to be white and color them, then all the black ones and color them. Note you only have to "check the list" 2 times in the second case but 25K times the first one.
我把这些放在评论中,但意识到它们构成了某种答案:
你说没有索引,但你说 ID 字段是 PK。如果是这样,这些字段上必须有唯一索引。如果没有,那么它们就不是真正的 PK,这可以解释为什么带有 WHERE 子句的版本比仅带有 JOIN 的版本更快。
另外,Google“Jet SHOWPLAN”,这样您就可以看到 Jet 查询优化器正在做什么,然后您就可以真正看到发生了什么。
使用索引,您将获得索引合并,并且速度应该相当快。如果没有他们,我不确定 Jet 将如何做到这一点。此外,如果您的 Y/N 字段被索引,可能会有所不同。建议永远不要对稀疏填充的字段(即基数较低的字段)建立索引,但我发现在 Jet/ACE 中索引布尔字段实际上可以带来显着的性能差异。
I put these in comments, but realize they constitute something of an answer:
You say there are no indexes, but you say the ID fields are PKs. If so, there has to be a unique index on those fields. If there isn't, then they aren't really PKs, and that might explain why the versions with WHERE clauses are faster than the version just with a JOIN.
Also, Google "Jet SHOWPLAN" so you can see what the Jet query optimizer is doing and then you can really see what's going on.
With indexes, you'll get an index merge and that should be pretty fast. Without them, I'm not sure how Jet would do it. Also, it might make a difference if your Y/N field is indexed. The powers that be advise to never index sparsely populated fields (i.e., fields with low cardinality), but I've found that index Boolean fields in Jet/ACE actually can make a significant performance difference.