I'm working on a project in which the previous programmer seems to have loved using the AS keyword in the SELECT part of queries. I really don't like how it's written but if there is no performance difference I won't change existing code.
My tests did give a slight advantage to not using the AS keyword but I'm clueless on how to properly test it(I ran the different queries using Navicat)
And maybe there is also a difference between different types of databases and engines. I'm using MySQL InnoDB.
Simplified query:
SELECT DISTINCT
`sometable`.`id` AS `id`,
`sometable`.`name` AS `name`,
`sometable`.`description` AS `description`,
`sometable`.`price` AS `price`,
`sometable`.`size` AS `size`,
`sometable`.`area` AS `area`,
`sometable`.`center` AS `center`
FROM `sometable`
-
2\$\begingroup\$ Since you are only changing the metadata, i.e. the name of the returned columns, it doesn't affect each row as it is processed. I'd expect the overhead to be inconsequential. \$\endgroup\$HABO– HABO2012年10月05日 13:09:18 +00:00Commented Oct 5, 2012 at 13:09
1 Answer 1
I wouldn't expect much if any noticeable overhead as all that is doing is aliasing the name of the column in the result set.
The question I would ask however, is why it's being done in the first place? All I can think of is to prevent the code which uses the query breaking if a column is renamed (as long as the alias isn't changed).
-
\$\begingroup\$ I have no idea why it was done, I suppose there is no good reason to it. I wonder, how would it not break this code if a column is renamed? You would still have to rename the key in the query itself so you could add the AS old_key when you do that. And please future programmers, add a comment why you added the alias and or changed the column name. \$\endgroup\$René– René2012年10月05日 18:51:29 +00:00Commented Oct 5, 2012 at 18:51
-
1\$\begingroup\$ Well I suppose that depends on where the query used, I was thinking that if it was in a stored procedure for example, if the column name in the table was changed, as long as the alias remained the same, the results of the stored procedure would remain consistent... \$\endgroup\$Trevor Pilley– Trevor Pilley2012年10月05日 18:57:32 +00:00Commented Oct 5, 2012 at 18:57
-
\$\begingroup\$ I didn't know/realized a stored procedure would do that. Thanks. Sadly, this is mysql_* in php. \$\endgroup\$René– René2012年10月05日 19:06:52 +00:00Commented Oct 5, 2012 at 19:06