1

i have this tabel,

CREATE TABLE `forum_rank` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL DEFAULT '0',
  `rank` int(11) NOT NULL DEFAULT '0',
  `forum_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

now i ask about what perfome best, its * or alle felt like this 2 eg.

select * form forum_rank;

or

select id, user_id, rank, forum_id from forum_rank;

2 Answers 2

4

You should explicitly specify the columns. Otherwise the database engine will first have to find out what the table's columns are (resolve the * operator) and after perform the actual query.

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

Comments

4

I don't think performance will be a problem here. There's a better reason to prefer the second idiom: your code is less likely to break if you add additional columns.

6 Comments

Hi, I might be missing something in here, but why queries using the * operator would break and those who don't wouldn't?
If you're writing Java, and you get values out of the ResultSet that depend on column index, a change in indexes will result in the wrong item being mapped into your object.
I am not sure I agree with you. If you add new columns to a table and use "select *", the new columns would be the last in the ResultSet so the first indexes should remain the same. Alternatively, if you select by column names and a add a new column in the select at any position but the last, this will indeed break the code. Retrieving data from ResultSets by index is considered a bad practice. As far as I know data should be retrieved by column names.
I agree with your point about retrieving by column names, but it's not universally done. It's certainly possible to use column indexes. I'm not aware that the new columns are guaranteed to be last, but I could be wrong.
@Florin Dumistrescu: How does select by column names and adding a new column break the code? If you select by name, you only keep what you want/need. If that new column is not needed, the code is not broken. If that column is needed, you better know the select you are doing. Perhaps this is Java specific?
|

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.