计算机应用 | 古代文学 | 市场营销 | 生命科学 | 交通物流 | 财务管理 | 历史学 | 毕业 | 哲学 | 政治 | 财税 | 经济 | 金融 | 审计 | 法学 | 护理学 | 国际经济与贸易
计算机软件 | 新闻传播 | 电子商务 | 土木工程 | 临床医学 | 旅游管理 | 建筑学 | 文学 | 化学 | 数学 | 物理 | 地理 | 理工 | 生命 | 文化 | 企业管理 | 电子信息工程
计算机网络 | 语言文学 | 信息安全 | 工程力学 | 工商管理 | 经济管理 | 计算机 | 机电 | 材料 | 医学 | 药学 | 会计 | 硕士 | 法律 | MBA
现当代文学 | 英美文学 | 通讯工程 | 网络工程 | 行政管理 | 公共管理 | 自动化 | 艺术 | 音乐 | 舞蹈 | 美术 | 本科 | 教育 | 英语 |

五种提高SQL性能的方法(一)毕业论文(2)

2013-07-22 01:14
导读:d) and interprets it as representing a Recordset. So the true data is pushed back into a second Recordset. While you can get to this second Recordset using the NextRecordset method in ADO, it is much
d) and interprets it as representing a Recordset. So the true data is pushed back into a second Recordset. While you can get to this second Recordset using the NextRecordset method in ADO, it is much easier (and more efficient) if you can always count on the Recordset being the first and only one returned.
 While this technique gets the job done, it does require extra code in the SQL statement. Another way of getting the same result is to use the SET NOCOUNT ON statement preceding the INSERT and to put the SELECT @@IDENTITY statement in a FOR INSERT trigger on the table, as shown in the following code snippet. This way, any INSERT statement into that table will automatically return the IDENTITY value.
 CREATE TRIGGER trProducts_Insert ON Products FOR INSERT AS
     SELECT @@IDENTITY
 GO
 The trigger only fires when an INSERT occurs on the Products table, so it always will return an IDENTITY after a successful INSERT. Using this technique, you can consistently retrieve IDENTITY values in the same manner across your application.
 Inline Views Versus Temp Tables
 Queries sometimes need to join data to other data that may only be gathered by performing a GROUP BY and then a standard query. For example, if you want to return the information about the five most recently placed orders, you would first need to know which orders they are. This can be retrieved by using a SQL query that returns the orders' IDs. This data could be stored in a temporary table, a common technique, and then joined to the Product table to return the quantity of products sold on those orders:
 CREATE TABLE #Temp1 (OrderID INT NOT NULL, _
                      OrderDate DATETIME NOT NULL)
 INSERT INTO #Temp1 (OrderID, OrderDate)
 SELECT     TOP 5 o.OrderID, o.OrderDate
 FROM Orders o ORDER BY o.OrderDate DESC 中国大学排名
 SELECT     p.ProductName, SUM(od.Quantity) AS ProductQuantity
 FROM     #Temp1 t
     INNER JOIN [Order Details] od ON t.OrderID = od.OrderID
     INNER JOIN Products p ON od.ProductID = p.ProductID
 GROUP BY p.ProductName
 ORDER BY p.ProductName
 DROP TABLE #Temp1
 This batch of SQL creates a temporary table, inserts the data into it, joins other data to it, and drops the temporary table. This is a lot of I/O for this query, which could be rewritten to use an inline view instead of a temporary table. An inline view is simply a query that can be joined to in the FROM clause. So instead of spending a lot of I/O and disk access in tempdb on a temporary table, you could instead use an inline view to get the same result:
 SELECT p.ProductName,
     SUM(od.Quantity) AS ProductQuantity
 FROM     (
     SELECT TOP 5 o.OrderID, o.OrderDate
     FROM     Orders o
     ORDER BY o.OrderDate DESC
     ) t
     INNER JOIN [Order Details] od ON t.OrderID = od.OrderID
     INNER JOIN Products p ON od.ProductID = p.ProductID
 GROUP BY
     p.ProductName
 ORDER BY
   &
上一篇:ASP动态网站建设论文(一)毕业论文 下一篇:没有了