开发者

Optimizing select SQL - MySQL

开发者 https://www.devze.com 2023-02-28 06:50 出处:网络
Plz help me on this Table Structure ID(primary key) Text To find a text what should i write ? select Text from table where ID=1

Plz help me on this

Table Structure

ID(primary key) Text

To find a text what should i write ?

select Text from table where ID=1  

开发者_Python百科or

select Text from table where ID=1 limit 1

Another Table Structure

Parent Table

PID(p k) ID

Child Table

ID (p.k) PID(f.k)

what should i write to delete fast ?

delete from ChildTable where ID=1;

or

delete from ChildTable where ID=1 and pid=1;  [i know the PID]


The first two statements are equivalent. It should never return more than one row, so adding limit 1 is redundant.

For the deletes, it shouldn't matter. Mysql should look up by ID first as it's the most efficient index. There should only be one row returned, and hence the PID column is redundant, and at worst could actually confuse mysql into using a less efficient query plan.


Just use the queries with the primary key only.

0

精彩评论

暂无评论...
验证码 换一张
取 消