0

Something that was always on my mind, but I never questioned: When I build queries, I always use all prefixes possible (e.g., Database.Table or Table.Column):

SELECT `User`.`ID` FROM `Shop`.`User` WHERE `User`.`Name` = "David" LIMIT 1 ;

instead of just:

SELECT `ID` FROM `User` WHERE `Name` = "David" LIMIT 1 ;

I do not know why, but it was always on my mind that if you use prefixes, this will increase the performance of the query.

Is that true or just a myth I had for years on my mind?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

You should test it yourself.

The answer you'll find is no (other than the query size being sent to the database). The table.column qualifier use is typically used when using more than one table in a query to prevent ambiguity.

For example, if you're joining two tables that both contain a column named userId, you would alias the column:

SELECT a.`userId`
FROM `user` a
LEFT JOIN `email` b
    ON b.`userId` = a.`userId`

If you were to only SELECT userId, you would receive this error.

Community
  • 1
  • 1
Kermit
  • 33,827
  • 13
  • 85
  • 121
  • I agree: The table qualifier is required when there is ambiguity. However, even when there is no ambiguity, the server will have to resolve each column's table. This operation shouldn't add any measurable overhead since schema info is almost always cached (depending on vendor). Other than that, adding qualifiers will certainly improve code readability. – Tasos P. Nov 24 '13 at 20:27