Do I need to write LIMIT 1
using PDO's fetch()
method? This assumes that I need only one result. I'm wondering, does using LIMIT 1
will help to save resources and loading time?
Asked
Active
Viewed 2,384 times
5

Gajus
- 69,002
- 70
- 275
- 438
-
Are you by chance downvoting every answer you receive? :-P – Álvaro González Jun 29 '11 at 17:22
-
strictly speaking, PDO::fetch() can be used with multiple rows as well, using the while loop. and in this case adding limit 1 will be counter-productive – Your Common Sense Apr 08 '22 at 07:56
1 Answers
1
The LIMIT
clause is just one more SQL feature you can use to retrieve the information you need. Asking whether you need LIMIT
cannot be answered unless you narrow your context. It's like asking if you need WHERE
or JOIN
. It depends on your data and your needs.
Not sure if that's what you have in mind, but I've seen projects where the developer was not fluent in SQL and just did the heavy lifting in application code, typically restricting their SQL to SELECT * FROM table
. I think it's worthwhile to learn the basics of SQL so you're able to return the exact rows and columns you need. That's often a great boost for performance.

Álvaro González
- 142,137
- 41
- 261
- 360