简体   繁体   English

优化没有索引的表的删除查询?

[英]optimizing a delete query for table with no index?

Let's say we have the following tables: r(a, b, c) where a is some integer, and the table has 50 rows s(d, e, f, a) sa is a foreign key onto ta each tuple is 400 bytes and s is a multiset since this is a SQL database. 假设我们有以下表:r(a,b,c)其中a是一些整数,并且该表有50行s(d,e,f,a)sa是ta上的外键,每个元组为400字节s是一个多集,因为这是一个SQL数据库。 s holds about 1 million tuples. s拥有大约一百万个元组。 when a table is added, the database server automatically creates an index on the primary-‐key columns in the table so for example t has an index on a. 添加表时,数据库服务器会在表的主键列上自动创建索引,例如t在a上具有索引。 However, Oracle does not create indexes on columns that are foreign keys. 但是,Oracle不会在作为外键的列上创建索引。 So s has no index. 所以s没有索引。 We want to delete 15 rows from t. 我们想从t删除15行。 So here is what we do: 1) We remove rows from s that have the same a values in t. 因此,我们要做的是:1)我们从s中删除与t中具有相同a值的行。 This takes 10 minutes. 这需要10分钟。 2) Then we run the statement DELETE FROM t WHERE a IN (values we are trying to remove, 15 of them to be exact) This takes about 6 hours. 2)然后我们运行语句DELETE FROM t WHERE a IN(我们尝试删除的值,准确地删除其中的15个值),这大约需要6个小时。

So my guess is the second step takes a long time because for every tuple in t, we are comparing the value of a to every value within the query list. 所以我的猜测是第二步需要很长时间,因为对于t中的每个元组,我们都将a的值与查询列表中的每个值进行比较。 So how can we optimize this query to get the same result but much faster!? 那么我们如何优化此查询以获得相同的结果却要快得多!

if you use joins For CRUD Operations will be faster rather than using IN or subquery 如果使用join for CRUD,操作将比使用IN或子查询更快

For eg: 例如:

DELETE FROM t 
INNER JOIN S ON t.a = S.a 

Hope this helps 希望这可以帮助

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

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