我有一个多到多的桌子,这将有数百万行。让我用一个例子来描述我的困惑。
示例:
表: car_dealer_rel
选择:1
列: car_id: int无符号,dealer_id: int无符号索引on: car_id,dealer_id
car_id|dealer_id
-------|---------
1 | 1
1 | 2
....选择-2:
单列表
car_id_dealer_id: varchar:21索引on: PKI在这一列上。
这里的想法是将值设置为:car_id.dealer_id,并将搜索作为%.xxx and or xxx.%
car_id_dealer_id
----------------
1.1
1.2
1.15
2.10
...
...在数以百万计的记录之后,这些记录将更快地用于:
我是MySQL的新手,非常感谢大家的帮助。
发布于 2017-09-10 18:21:39
第一个
car_id|dealer_id
-------|---------
1 | 1
1 | 2您可以轻松地为双方创建复合索引。
create index ind1 on car_dealer_rel (car_id,dealer_id );
create index ind2 on car_dealer_rel (dealer_id, car_id ); 它工作得很快
在这两种意义上你都可以很容易地过滤
where car_id = your_value 或
where dealer_id = another_value 或者两者兼用
对于第二个问题,您不能很容易地做到这一点(经常需要字符串操作,这不允许使用索引),而且在某些情况下不能使用sql
对于更新,插入和删除性能在实际中保持不变。
发布于 2017-09-10 18:26:33
这取决于您使用的实际查询,我建议先运行EXPLAIN,使用相当多的虚拟数据,以了解MySQL将如何执行您的查询。
但是,如果仅通过列car_id或car_id 和 dealer_id来查找记录,则可以使用复合索引(car_id、dealer_id)。
如果还想单独通过dealer_id查找,可以在dealer_id列上添加其他索引。
单列表选项不是很好,因为
dealer_id快速找到行。https://stackoverflow.com/questions/46144000
复制相似问题