`
txf2004
  • 浏览: 6973620 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用WHERE语句对SQL进行基础性的优化

阅读更多

使用WHERE语句对SQL进行基础性的优化

【我首先要抱怨一下这个该死的CSDN博客,我辛辛苦苦翻译完成的东西,提交上去居然丢了,想杀人了】

【还有这该死的博客升级,升的是一塌糊涂,考虑换地方ING.....................】

我最近工作于一个拍卖网站的项目,由于在后期发现站点的数据库出现了过载现象,于是我想到了对数据库查询进行优化

使用WHERE语句对SQL进行优化

经过我的研究,发现主页上的一条语句执行居然要花近5s不可忍受,于是我就查看这条SQL语句了

SELECT I.itemId IitemId, max(B.amount) bidAmount, I.name, max(B.bidDate) lastBid, I.value, Ca.name CaName, Ca.categoryId, I.pictureThumbnail, Co.logoThumbnail, Co.name CoName, Co.companyId CoCompanyId, B.username
FROM item I
LEFT JOIN category Ca ON Ca.categoryId=I.categoryId
LEFT JOIN company Co ON Co.companyId=I.companyId
LEFT JOIN (SELECT itemId, amount, bidDate, username FROM bid B LEFT JOIN account A ON A.accountId=B.accountId ORDER BY amount DESC) B ON B.itemId=I.itemId
GROUP BY I.itemId
ORDER BY B.bidDate DESC
LIMIT 20

其实在刚开始的时候由于数据量比较少用户使用的时候也没有问题,但是到最后,成交表里有接近20000条数据,这个时候查询的速度就非常 慢了

我做了什么呢,不过是加了条WHERE 语句,筛选最近一小时内的记录

SELECT I.itemId IitemId, max(B.amount) bidAmount, I.name, max(B.bidDate) lastBid, I.value, Ca.name CaName, Ca.categoryId, I.pictureThumbnail, Co.logoThumbnail, Co.name CoName, Co.companyId CoCompanyId, B.username
FROM item I
LEFT JOIN category Ca ON Ca.categoryId=I.categoryId
LEFT JOIN company Co ON Co.companyId=I.companyId
LEFT JOIN (SELECT itemId, amount, bidDate, username FROM bid B LEFT JOIN account A ON A.accountId=B.accountId ORDER BY amount DESC) B ON B.itemId=I.itemId
WHERE B.bidDate>’2007-11-20 12:00:20′
GROUP BY I.itemId
ORDER BY B.bidDate DESC
LIMIT 20

为什么可以这么做呢?因为流量非常大,我可以有充分的自信1小时内绝对有20比单子

当然你也可以做索引优化查询的时间。
【补充:我原来的翻译比这要翔实多了,怪只能怪万恶的CSDN 博客系统】

Basic SQL Query Optimization with WHERE Clauses

I recently worked on an auction site that was very database-heavy. Because of the amount of traffic and number of connections needed per page, the database began to get overwhelmed. One of the first steps to improve performance of your site if it is getting bogged down by traffic is to take a look at your database connections and query optimization – you may also want to upgrade server hardware, which I would always recommend in complement to optimizing code.

Optimize SQL Queries With Where Clauses

The first query I noticed was a query on the home page of the auction that listed the most recent bids in the auction. The query grabbed the item details, and highest bidding information from the database for the 20 most recent bids in the site. I grabbed the twenty most recent records from the bid table and then join a bunch of other tables to get the item, company, and user details. Here it is:

SELECT I.itemId IitemId, max(B.amount) bidAmount, I.name, max(B.bidDate) lastBid, I.value, Ca.name CaName, Ca.categoryId, I.pictureThumbnail, Co.logoThumbnail, Co.name CoName, Co.companyId CoCompanyId, B.username
FROM item I
LEFT JOIN category Ca ON Ca.categoryId=I.categoryId
LEFT JOIN company Co ON Co.companyId=I.companyId
LEFT JOIN (SELECT itemId, amount, bidDate, username FROM bid B LEFT JOIN account A ON A.accountId=B.accountId ORDER BY amount DESC) B ON B.itemId=I.itemId
GROUP BY I.itemId
ORDER BY B.bidDate DESC
LIMIT 20

When I grabbed the database and code from the server and set it up on my laptop for testing, I found that running this query through mysql took 4.78 seconds! That meant every time the page would load the page would take 4.78 seconds to grab the data it needed from the database, among other database queries included on the page. Keep in mind, that MySQL will cache query results based on the settings in your my.cnf file, so if the results from this query didn’t change, it would be lightning quick for the next person to load the page, however, any time a new bid was placed, these results would change and the cached query would not be used. During peak levels of the auction we had close to a bid every 15-30 seconds so this was definitely slowing down the page and being the home page, a lot of users noticed. There was no problem at all when the auction started, but because the bid table had close to 20000 bids by the end of the auction it really slowed down.

What did I do to speed it up? All I added was a simple WHERE clause – WHERE B.bidDate>’2007-11-20 12:00:20′ – dynamically generating the date stamp to 1 hour before the current time.

SELECT I.itemId IitemId, max(B.amount) bidAmount, I.name, max(B.bidDate) lastBid, I.value, Ca.name CaName, Ca.categoryId, I.pictureThumbnail, Co.logoThumbnail, Co.name CoName, Co.companyId CoCompanyId, B.username
FROM item I
LEFT JOIN category Ca ON Ca.categoryId=I.categoryId
LEFT JOIN company Co ON Co.companyId=I.companyId
LEFT JOIN (SELECT itemId, amount, bidDate, username FROM bid B LEFT JOIN account A ON A.accountId=B.accountId ORDER BY amount DESC) B ON B.itemId=I.itemId
WHERE B.bidDate>’2007-11-20 12:00:20′
GROUP BY I.itemId
ORDER BY B.bidDate DESC
LIMIT 20

Because I knew there would be bids every few seconds, I could rely on their being plenty of bids each hour to fill up the 20 most recently bid on items. The difference in query time? Adding the WHERE clause reduced the query speed to 0.45 seconds. That’s a huge increase in performance – 4.78 seconds to 0.45 seconds.

I wanted to show the increase in performance by optimizing the SQL query here, but you could also take a look at what indexes you have setup on your database tables to help further improve performance of your database.

分享到:
评论

相关推荐

    SQL语句基础教程

    * 数据聚合:使用GROUP BY和HAVING语句对数据进行聚合 SQL语法详解 ------------- ### SELECT指令 SELECT指令是SQL语言的基础,用于从数据库中的表格内选出资料。SELECT指令的语法结构如下: SELECT "栏位名" ...

    非常好用的SQL语句优化34条+sql语句基础

    以下是对"非常好用的SQL语句优化34条+sql语句基础"这一主题的详细解析: 1. **索引优化**:索引是提高查询速度的关键。创建合适的索引(主键、唯一索引、全文索引等)能显著提升数据检索效率。但同时要注意,过多的...

    最新整理的常用sql语句及优化大全

    本资源“最新整理的常用sql语句及优化大全”涵盖了SQL的基础使用和性能优化,对于数据库管理员、开发人员或是学习者来说,都是极具价值的学习资料。 一、SQL常用语句 1. **数据查询**:`SELECT`语句是SQL中最基本...

    SQL语句优化,语法优化

    SQL语句优化是数据库性能提升的关键环节,尤其是在大数据量的环境下。优化SQL语句能够显著提高查询速度,减少...同时,监控和分析SQL执行计划也是优化过程中的重要步骤,它能帮助我们找出性能瓶颈并针对性地进行改进。

    SQL语句基础PPT

    **一、SQL基础语法** 1. **数据类型**: SQL中的数据类型包括数值型(如INT, FLOAT)、字符串型(CHAR, VARCHAR)、日期时间型(DATE, TIME, DATETIME)等,理解这些数据类型对于创建表和存储数据至关重要。 2. **...

    sql基础与优化吐血整理

    一、SQL基础 1. SQL命令:SQL提供了多种命令来操作数据库,如SELECT(获取数据)、UPDATE(更新数据)、DELETE(删除数据)、INSERT INTO(插入数据)、CREATE DATABASE(创建数据库)、ALTER DATABASE(修改数据库...

    SQL基本语句 SQL基本语句

    了解并熟练掌握这些基本的SQL语句是数据库管理的基础,也是成为有效数据操作者的关键。随着进一步的学习,你可以探索更多高级的SQL特性,如存储过程、触发器和视图,以提升数据库管理和分析的能力。

    通过分析SQL语句的执行计划优化SQL

    ### 通过分析SQL语句的执行计划优化SQL 在数据库管理与开发过程中,SQL语句的性能优化至关重要。本文将详细介绍如何理解SQL及其执行计划,并给出具体的优化策略。通过优化SQL,可以显著提升应用程序的响应速度及...

    sql语句优化技术分析

    通过以上策略,我们可以对SQL语句进行有效优化,提高数据库系统的整体性能。然而,优化是一个持续的过程,需要根据实际情况不断调整和改进。在实际工作中,结合数据库管理系统的特性和具体业务需求,才能实现最佳的...

    # sql基础与优化

    标题中的“# sql基础与优化”表明我们...以上就是“sql基础与优化”的一些关键知识点,包括SQL的基本操作和在Oracle数据库中使用索引的注意事项。理解并掌握这些,能够显著提高SQL查询的效率,从而提升整体系统性能。

    《Effective MySQL之SQL语句最优化》数据库SQL

    综上所述,《Effective MySQL之SQL语句最优化》涵盖了SQL优化的各个方面,从基础的索引使用到高级的查询优化技巧,再到数据库设计和参数调整,都是提升数据库性能的重要知识点。通过学习和实践,我们可以有效地优化...

    sql语句、动态SQL语句基本语法

    - 避免全表扫描:尽量使用WHERE子句限制查询范围,避免对大量数据进行全表扫描。 - 分析查询计划:通过分析查询计划找出性能瓶颈并优化SQL语句。 - 减少子查询:尽可能将子查询转化为JOIN操作,以减少查询复杂度。 ...

    SQL常用优化脚本,优化SQL语句

    这些脚本不仅提供了数据库性能优化的基础工具,也揭示了SQL Server内部的工作机制,对于提高数据库效率和稳定性具有重要作用。通过定期执行这些脚本,可以及时发现并解决潜在的性能瓶颈,确保数据库系统的健康运行。

    Delphi中sql语句的使用总结

    在Delphi中使用SQL语句是与数据库进行交互的重要手段之一。本文将详细介绍如何在Delphi环境中构造和执行SQL查询,并给出具体的示例来帮助理解。 #### 一、基本SQL查询的构建 在Delphi中,通过`TADOQuery`组件来...

    SQL语句优化总结

    SQL语句优化是一项重要的数据库管理技能,能够显著提高数据库查询性能,减少资源消耗,缩短响应时间。以下是对上述文件内容中提及的关键知识点的详细总结: 1. 选择最有效的表名顺序 在使用基于规则的优化器时,...

    使用SQL语句批量更新数据.rar

    通过理解并熟练掌握这些知识点,你将能够高效且安全地使用SQL语句进行批量更新,提升数据库管理效率。在实践中,还应结合具体数据库管理系统(如MySQL、Oracle、SQL Server等)的特性,以获得最佳效果。

    sql经典基础语句集

    在“sql经典基础语句集”中,我们可以期待找到一系列关于SQL核心概念和基本操作的知识点。 1. **SQL 数据类型**:SQL支持多种数据类型,如整型(INTEGER)、浮点型(FLOAT)、字符串(VARCHAR)、日期/时间类型...

    SQL语句使用教程

    你可以根据需要选择特定的列,使用WHERE子句进行条件过滤,GROUP BY进行数据分组,HAVING来筛选分组后的结果,ORDER BY进行排序,以及LIMIT来限制返回的行数。 2. INSERT语句:当你需要向数据库中添加新记录时,...

    SQLServer实用SQL语句大全

    第3章:T-SQL基础 该章是学习SQL的基础,涵盖了SELECT语句的使用,如查询、排序、分组、聚合函数等,还有WHERE、HAVING子句用于数据过滤。 第4章:DML操作 本章讲解INSERT、UPDATE和DELETE语句,用于数据的插入、...

Global site tag (gtag.js) - Google Analytics