duai1683 2014-08-23 09:13
浏览 451
已采纳

在查询中不使用空格和短表别名有什么好处吗?

Do queries with no excessive spaces and small table aliases stress the MySQL server less or is the difference negligible?

SELECT my_table.a, my_table.b, my_table.c, my_table.d
FROM table_containing_data as my_table
WHERE my_table.a = 1 AND my_table.b = 1 AND my_table.c = 1 AND my_table.d = 1

vs.

SELECT t.a,t.b,t.c,t.d
FROM table_containing_data t
WHERE t.a=1 AND t.b=1 AND t.c=1 AND t.d=1

I am fully aware of the readability of each of them. This question is purely hypothetical. If the difference (when running queries like these hundreds of thousands of times a day) is significant, I might change how my query engine works.

  • 写回答

1条回答 默认 最新

  • doupa1883 2014-08-23 09:19
    关注

    Even for billions of queries, whitespace in your queries is not going to make the difference.

    Databases cache queries and execution plans anyway, so if you end up sending the same query over and over again (why aren't you using prepared statements?) any decent database will not have to parse the whole thing again. Of course that only applies if you don't mix SQL and data in the same string.. which is usually done with MySQL. So here's another reason to use prepared statements!

    Also, compared to actually retrieving data from the disk (which, by the way, should happen rarely if your database is not huge - you want LOTS of RAM in your database machines to have as much as possible in memory) any parsing etc. is negligible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本