简体   繁体   English

一对一关系数据库关系的可能优化

[英]Possible optimizations of one-to-one relational database relationships

What are the strategies for optimizing one-to-one database joins, except indexing and possibly merging the tables? 除了建立索引并可能合并表以外,还有什么策略可以优化一对一的数据库联接?

Assume the tables contain 1 million rows in each. 假设每个表包含一百万行。

Indexing your tables like you mentioned is already pretty good, but physically ordering table on disk according to an index order makes it almost ideal. 像您提到的那样对表进行索引已经相当不错了,但是根据索引顺序在磁盘上对表进行物理排序使它几乎是理想的。

For this, you can create clustered indexes in SQL Server (clustered indexes are also supported on some other databases like PostgreSQL ). 为此,您可以在SQL Server中创建聚簇索引 (某些其他数据库(如PostgreSQL )也支持聚簇索引)。 Unfortunately, SQLite does not support clustered indexes. 不幸的是,SQLite不支持聚簇索引。 Obviously, only one index can be clustered - basically, this is the one that table pages will try to be physically ordered by on disk. 显然,只能聚集一个索引-基本上,这是表页将尝试按磁盘物理排序的索引。

Also, on all databases (including SQLite), you can simply copy your table while reordering it according to the index, using something like 此外,在所有数据库(包括SQLite)上,您可以简单地复制表,同时根据索引对表重新排序,例如

CREATE TABLE mytable_ordered AS
SELECT * FROM mytable
ORDER BY key_column;

DROP TABLE mytable;

ALTER TABLE mytable_ordered RENAME TO mytable;

CREATE INDEX mytable_key_column_idx ON mytable (key_column);

You should do the same on another big table you mentioned. 您应该在提到的另一张大桌子上做同样的事情。 After this, joining these tables by ordered key 1:1 should be as fast as it could possibly get (short of merging these tables into one). 此后,通过有序键1:1联接这些表应尽可能快(将这些表合并为一个表)。

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

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