0

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  

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]

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think you got the DELETE query wrong. ID is the primary key.
i know limit 1 does not help anything but will it save some time too ? I think you got wrong on the 2nd query, plz read it again !
You're right. I read ID and PID wrong. I updated my answer. No, limit 1 won't save any time if it's only going to try to return 1 row anyway. It doesn't do anything in your case.
1

Just use the queries with the primary key only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.