This question already has answers here:
Why is SELECT * considered harmful?

(15个答案)


7年前关闭。




我知道一般来说,构建mysql查询来命名您需要的每个项目总是更好的性能,但是例如在配置文件页面上,我可能需要除少数项目之外的所有项目。
    SELECT user_name,f_name,l_name,country,usa_state,other_state,zip_code,city,gender,birth_date,date_created,date_last_visit,
user_role,photo_url,user_status,friend_count,comment_count,forum_post_count,referral_count,referral_count_total,
setting_public_profile,setting_online,profile_purpose,profile_height,profile_body_type,profile_ethnicity, profile_occupation,profile_marital_status,profile_sex_orientation,profile_home_town,profile_religion,
profile_smoker,profile_drinker,profile_kids,profile_education,profile_income,profile_headline,profile_about_me,
profile_like_to_meet,profile_interest,profile_music,profile_television,profile_books,profile_heroes,profile_here_for,profile_counter FROM users WHERE user_id=1 AND user_role >

因此,如果不进行大量测试,也许有更多经验的人可以提出一些建议?

这会更糟吗
SELECT * FROM users WHERE user_id=1 AND user_role >

我更喜欢列出所有项目,因为如果我需要数据库中的东西,那么在该页面上可以轻松查看我可以得到的东西,但是如果它更快,那么我就不会列出它们

最佳答案

注意:命名所有字段当然是最佳实践,但是在本文中,我将仅讨论性能优势,而不是设计或维护优势。

出于以下原因,*语法可能会变慢:

  • 并非所有字段都已建立索引,并且查询使用全表扫描。可能不是您的情况:您返回的所有字段几乎都不可能用单个索引编制索引。
  • 从包含可变长度列的表中返回尾随字段可能会导致轻微的搜索开销:要返回20th字段,应检查以前的19并计算偏移量。
  • 仅需要返回更多数据(通过连接传递)。

  • 由于您几乎需要所有字段,因此最后一个原因可能是最重要的一个。假设description TEXT字段只能是页面上未使用的1字段的50,但可以占据10倍于所有其他字段的空间。

    在这种情况下,最好为所有字段命名,并省略不需要的长字段。

    关于sql - 最好在MySQL中调用很多字段时使用*? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1960036/

    10-11 02:53
    查看更多