网站地图| 免费获取|
免费论文网
  • 网站首页|
  • 论文范文|
  • 修改降重|
  • 职称论文|
  • 合作期刊|
  • 论文下载|
  • 计算机论文|
  • 外文翻译|
  • 免费论文|
  • 原创论文|
  • 开题报告论文
搜索

当前位置:免费论文网 -> 免费论文 -> 计算机论文 -> 五种提高SQL性能的方法(四)
计算机论文| ASP设计| Delphi| VB设计| JSP设计| ASP.NET设计| VB.NET| java设计| VC| pb| VS| dreamweaver| c#.net| vf| VC++| 计算机论文范文| 论文下载| 自动化论文

五种提高SQL性能的方法(四)

五种提高SQL性能的方法(四) d up when you have many users accessing an online database application.
 Use Cartesian Products Wisely
 For this tip, I will go against the grain and advocate the use of Cartesian products in certain situations. For some reason, Cartesian products (CROSS JOINS) got a bad rap and developers are often cautioned not to use them at all. In many cases, they are too costly to use effectively. But like any tool in SQL, they can be valuable if used properly. For example, if you want to run a query that will return data for every month, even on customers that had no orders that particular month, you could use a Cartesian product quite handily. The SQL in Figure 2 does just that.
 While this may not seem like magic, consider that if you did a standard INNER JOIN from Customers to Orders, grouped by the month and summed the sales, you would only get the months where the customer had an order. Thus, you would not get back a 0 value for the months in which the customer didn't order any products. If you wanted to plot a graph per customer showing every month and its sales, you would want the graph to include 0 month sales to identify those months visually. If you use the SQL in Figure 2, the data skips over the months that had $0 in sales because there are no rows in the Orders table for nonsales (it is assumed that you do not store what did not occur).
 The code in Figure 3 is longer, but can achieve the same goal of getting all the sales data, even for months without sales. First, it grabs a list of all of the months in the past year and puts them in the first TABLE datatype table (@tblMonths). Next, the code gets a list of all customers' company names who had sales during that time period and puts them in another TABLE datatype table (@tblCus-tomers). These two tables store all of the basic data required to create the resultset except the actual sales numbers.
 All of the months are listed in the first table (12 rows) and all of the customers who had sales in that time frame are listed in the second table (81 for me). Not every customer purchased a product in each of the past 12 months, so performing an INNER or LEFT JOIN won't return every customer for every month. These operations will only return the customers and the months when they did purchase something.
 A Cartesian product can return all customers for all months. A Cartesian product basically multiplies the first table by the second table and results in a rowset that contains the number of rows in the first table times the number of rows in the second table. Thus, the Cartesian product returns 972 rows into the table @tblFinal. The last steps are to update the table @tblFinal with the monthly sales totals for each customer during the date range and to select the final rowset.
 Use CROSS JOINs with caution if you do not need a true Cartesian product because they can be very resource intensive. For example, if you do a CROSS JOIN on products and categories and then use a WHERE clause, DISTINCT or GROUP BY to filter out most of the rows, you could have gotten to the same result in a much more efficient manner by using an INNER JOIN. Cartesian products can be very useful when you need the data returned for all possibilities, as in the case when you want to load a graph with monthly sales dates. But you should not use them for other purposes as INNER JOINs are much more efficient in most scenarios.
 Odds and Ends
 Here are a few other common techniques that can help improve the efficiency of your SQL querying. Let's assume you are going to group all of your salespeople by region and sum t

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 4/7/7

五种提高SQL性能的方法(四)由免费论文网(www.jaoyuw.com)会员上传。
原创论文流程 相关论文
上一篇:ASP动态网站建设论文 下一篇:基于Web的网上评教系统
推荐论文 本专业最新论文
Tags:提高 SQL 性能 方法 2010-03-27 21:52:08【返回顶部】
精彩推荐
发表论文

联系方式 | 论文说明 | 网站地图 | 免费获取 | 钻石会员 | 硕士论文


免费论文网提供论文范文,论文代发,原创论文

本站部分文章来自网友投稿上传,如发现侵犯了您的版权,请联系指出,本站及时确认并删除  E-mail: 17304545@qq.com

Copyright@ 2009-2020 免费论文网 版权所有 湘ICP备19027999