作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
The database仅包含 2 个表:
select * from transaction t
join wallet s on t.sender_id=s.id
join wallet r on t.receiver_id=r.id
limit 10;
> explain select * from transaction t
-> join wallet s on t.sender_id=s.id
-> join wallet r on t.receiver_id=r.id
-> limit 10;
tree | field | description
+---------------------+--------------------+----------------------+
| distributed | true
| vectorized | false
limit | |
│ | count | 10
└── hash-join | |
│ | type | inner
│ | equality | (receiver_id) = (id)
│ | right cols are key |
├── hash-join | |
│ │ | type | inner
│ │ | equality | (sender_id) = (id)
│ │ | right cols are key |
│ ├── scan | |
│ │ | table | transaction@primary
│ │ | spans | ALL
│ └── scan | |
│ | table | wallet@primary
│ | spans | ALL
└── scan | |
| table | wallet@primary
| spans | ALL
最佳答案
看起来这实际上是由于 SQL Server 和 CockroachDB 之间的查询计划不同,但可以通过几种方式解决。
根本问题在于transaction
表有两个外键约束指向 wallet
表,但两个外键都可以为空。这可以防止 CockroachDB 通过连接推送 10 行限制,因为对 transaction
的扫描table 可能需要为整个查询生成 10 多行以生成最多 10 行。
我们在查询计划中看到了这一点:
> explain select * from transaction t
join wallet s on t.sender_id=s.id
join wallet r on t.receiver_id=r.id
limit 10;
info
---------------------------------------------
distribution: full
vectorized: true
• limit
│ count: 10
│
└── • lookup join
│ table: wallet@primary
│ equality: (receiver_id) = (id)
│ equality cols are key
│
└── • lookup join
│ table: wallet@primary
│ equality: (sender_id) = (id)
│ equality cols are key
│
└── • scan
estimated row count: 10,000
table: transaction@primary
spans: FULL SCAN
请注意,在两个连接之后都会应用限制。
joins
与
left joins
.这将允许将限制下推到
transaction
上的扫描。 table 因为左连接永远不会丢弃行。
> explain select * from transaction t
left join wallet s on t.sender_id=s.id
left join wallet r on t.receiver_id=r.id
limit 10;
info
----------------------------------------
distribution: full
vectorized: true
• lookup join (left outer)
│ table: wallet@primary
│ equality: (receiver_id) = (id)
│ equality cols are key
│
└── • lookup join (left outer)
│ table: wallet@primary
│ equality: (sender_id) = (id)
│ equality cols are key
│
└── • scan
estimated row count: 10
table: transaction@primary
spans: LIMITED SCAN
limit: 10
另一种选择是在外键约束中设置引用列
non null
.这也将允许将限制下推到
transaction
上的扫描。表,因为即使是内部联接也永远不会丢弃行。
> alter table transaction alter column sender_id set not null;
ALTER TABLE
> alter table transaction alter column receiver_id set not null;
ALTER TABLE
> explain select * from transaction t
join wallet s on t.sender_id=s.id
join wallet r on t.receiver_id=r.id
limit 10;
info
----------------------------------------
distribution: full
vectorized: true
• lookup join
│ table: wallet@primary
│ equality: (receiver_id) = (id)
│ equality cols are key
│
└── • lookup join
│ table: wallet@primary
│ equality: (sender_id) = (id)
│ equality cols are key
│
└── • scan
estimated row count: 10
table: transaction@primary
spans: LIMITED SCAN
limit: 10
关于sql-server - 同样的 SQL 请求,CockroachDB 需要 4min SQL Server 需要 35ms。我错过了什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61645359/
我无法在附加行中显示“真”、“假”、"is"和“否”按钮。 我在这里有一个应用程序:Application 请按照以下步骤使用应用程序: 1。当你打开应用程序时,你会看到一个绿色的加号按钮,点击 在此
我是一名优秀的程序员,十分优秀!