avatar
Join optimization# Database - 数据库
a*s
1
I have two tables. table email has 1 million records. table domain has 20
records. domain is part of email address.
for example: email: a*******[email protected]
domain: yahoo.com
create table email(email varchar(40) primary key, domain varchar(20), index(
domain));
create table domain(domain varchar(20) primary key);
case1:
select p.email from email p, domain q where p.domain = q.domain
select p.email from email p, domain q where q.domain = p.domain
Is it the same?
Does the databa
avatar
B*g
2
the answer for these questions is "depends".
Without talking about more database structure like index, and without analys
is the real data, can not answer this questions.
但是一般来说,最好不要太依赖database。你是最了解你自己的table的,而不是datab
ase

index(

【在 a*******s 的大作中提到】
: I have two tables. table email has 1 million records. table domain has 20
: records. domain is part of email address.
: for example: email: a*******[email protected]
: domain: yahoo.com
: create table email(email varchar(40) primary key, domain varchar(20), index(
: domain));
: create table domain(domain varchar(20) primary key);
: case1:
: select p.email from email p, domain q where p.domain = q.domain
: select p.email from email p, domain q where q.domain = p.domain

avatar
a*t
3
you can see the query excution plan to see how the db engine optimize the
execution
in most cases, it does a good job

index(

【在 a*******s 的大作中提到】
: I have two tables. table email has 1 million records. table domain has 20
: records. domain is part of email address.
: for example: email: a*******[email protected]
: domain: yahoo.com
: create table email(email varchar(40) primary key, domain varchar(20), index(
: domain));
: create table domain(domain varchar(20) primary key);
: case1:
: select p.email from email p, domain q where p.domain = q.domain
: select p.email from email p, domain q where q.domain = p.domain

avatar
a*s
4
In MySQL, I use the explain to see how it works.
It seems that primary key has higher priority that index(non primary or
unique keys). It is reasonable.
Thanks

【在 a*******t 的大作中提到】
: you can see the query excution plan to see how the db engine optimize the
: execution
: in most cases, it does a good job
:
: index(

avatar
B*g
5
呵呵,primary key一般都有index

【在 a*******s 的大作中提到】
: In MySQL, I use the explain to see how it works.
: It seems that primary key has higher priority that index(non primary or
: unique keys). It is reasonable.
: Thanks

avatar
b*e
6
IN SQL, SET Statistics IO ON, will show the results.
avatar
x*e
7

index(
I have to say you need to read some book before posting questions.
Yes, they are the same, cause they are inner joins.
If outer join, it depends.
some recommendations for you.
avoid using varchar as primary key.
try to have ID columns instead.
like email(email_key, email, domain_key)
domain(domain_key, domain)
all key columns should be int,which is the best for index.
domain table does not need primary key or index, since it is just too small.

【在 a*******s 的大作中提到】
: I have two tables. table email has 1 million records. table domain has 20
: records. domain is part of email address.
: for example: email: a*******[email protected]
: domain: yahoo.com
: create table email(email varchar(40) primary key, domain varchar(20), index(
: domain));
: create table domain(domain varchar(20) primary key);
: case1:
: select p.email from email p, domain q where p.domain = q.domain
: select p.email from email p, domain q where q.domain = p.domain

avatar
B*g
8
wk,你们翻老贴。
其实在email table 给domain加个index就行了,1M record也不是很多

20
small.

【在 x***e 的大作中提到】
:
: index(
: I have to say you need to read some book before posting questions.
: Yes, they are the same, cause they are inner joins.
: If outer join, it depends.
: some recommendations for you.
: avoid using varchar as primary key.
: try to have ID columns instead.
: like email(email_key, email, domain_key)
: domain(domain_key, domain)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。