怎么优化这样的SQL让执行更快?
Custom_Money = curArrearage
End Select
End Function
Private Sub RefillList(ByVal lv As ListView, ByVal strType As String)
Dim clm As ColumnHeader
Dim itm As ListItem
Dim i As Long, j As Long
Dim nSecondBuy As String
Dim nArr As String
lv.ListItems.Clear
lv.View = lvwReport
lv.LabelEdit = lvwManual
Call objConn
Dim rsCustom As New ADODB.Recordset
Dim strListSql As String
rsCustom.CursorLocation = adUseClient
If strType = "Search" Then
strListSql = "SELECT * FROM Custom WHERE SubCorp='" & CurrentSubCorp & "' AND (CustomNO Like '%" & SafeSQL(Trim(txtSearch(0).Text)) & "%') AND (M_Name Like '%" & SafeSQL(Trim(txtSearch(1).Text)) & "%') AND (F_Name Like '%" & SafeSQL(Trim(txtSearch(2).Text)) & "%') AND (CardNO Like '%" & SafeSQL(Trim(txtSearch(3))) & "%') ORDER BY ID DESC"
Else
strListSql = "SELECT * FROM Custom WHERE SubCorp='" & CurrentSubCorp & "' ORDER BY ID DESC"
End If
rsCustom.Open strListSql, conn, 1, 1
If rsCustom.EOF = True And rsCustom.BOF = True Then Exit Sub
rsCustom.MoveLast
nItems = rsCustom.RecordCount
If nItems > 30 Then PanelReadDatabase.Visible = True: MDIMain.MousePointer = 11: DoEvents
rsCustom.MoveFirst
For i = 1 To nItems
nSecondBuy = Custom_Money(rsCustom.Fields("CustomNO"), "二次消费")
nArr = Custom_Money(rsCustom.Fields("CustomNO"), "欠款")
nCustomNO$ = "" & rsCustom.Fields("CustomNO")
nSeriesType$ = "" & rsCustom.Fields("SeriesType")
nCustomName = "" & rsCustom.Fields("M_Name") & " " & rsCustom.Fields("F_Name")
nCustomMobile = "" & rsCustom.Fields("M_Mobile") & " " & rsCustom.Fields("F_Mobile")
nCustomWork = "" & rsCustom.Fields("M_Work")
nSeries = "" & rsCustom.Fields("Series")
nSeriesPrice = "" & rsCustom.Fields("Receivable")
nCardNO = "" & rsCustom.Fields("CardNO")
nReturnMoney = "" & rsCustom.Fields("InReturnMoney")
Set itm = lv.ListItems.Add(, "Row" & i, rsCustom.Fields(0))
itm.SubItems(1) = Trim(nCustomNO)
itm.SubItems(2) = Trim(nSeriesType)
itm.SubItems(3) = Trim(nCustomName)
itm.SubItems(4) = Trim(nCustomMobile)
itm.SubItems(5) = Trim(nCustomWork)
itm.SubItems(6) = Trim(nSeries)
itm.SubItems(7) = Trim(nSeriesPrice)
itm.SubItems(8) = nSecondBuy
itm.SubItems(9) = nArr
itm.SubItems(10) = Trim(nCardNO)
itm.SubItems(11) = nReturnMoney
rsCustom.MoveNext
Next i
PanelReadDatabase.Visible = False
MDIMain.MousePointer = 0
rsCustom.Close
Set rsCustom = Nothing
TabSeries.Tabs(1).Caption = "共搜索相关顾客资料 " & lstCustom.ListItems.count & " 个"
'Set conn = Nothing
End Sub
Private Sub Form_Load()
'列取顾客资料
Call RefillList(lstCustom, "")
End Sub
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Custom]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Custom]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Money]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Money]
GO
CREATE TABLE [dbo].[Custom] (
[ID] [bigint] IDENTITY (1, 1) NOT NULL ,
[CustomNO] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[SubCorp] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[SeriesType] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[SeriesGroup] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[Series] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[Amount] [int] NULL ,
[SeriesPrice] [money] NULL ,
[TotalPrice] [money] NULL ,
[M_Name] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[M_Birthday] [datetime] NULL ,
[M_BirthdayCN] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[M_Phone] [varchar] (16) COLLATE Chinese_PRC_CI_AS NULL ,
[M_Mobile] [varchar] (12) COLLATE Chinese_PRC_CI_AS NULL ,
[M_Address] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[M_Zip] [varchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
[M_Work] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[F_Name] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[F_Birthday] [datetime] NULL ,
[F_BirthdayCN] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[F_Phone] [varchar] (16) COLLATE Chinese_PRC_CI_AS NULL ,
[F_Mobile] [varchar] (12) COLLATE Chinese_PRC_CI_AS NULL ,
[F_Address] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[F_Zip] [varchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
[F_Work] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[OrderDate] [datetime] NULL ,
[PhotoDate] [datetime] NULL ,
[IsPhotoExt] [bit] NULL ,
[PhotoExtPlace] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[PhotoExtDate] [datetime] NULL ,
[SelectDate] [datetime] NULL ,
[DigitalSelectDate] [datetime] NULL ,
[TakeOrderDate] [datetime] NULL ,
[TakeDate] [datetime] NULL ,
[MarryDate] [datetime] NULL ,
[MarryDateCN] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[Exigence] [bit] NULL ,
[VIP] [bit] NULL ,
[Abate] [money] NULL ,
[Receivable] [money] NULL ,
[OrderPay] [money] NULL ,
[SelectAdd] [money] NULL ,
[Arrearage] [money] NULL ,
[Source] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[CardNo] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Zone] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[InNo] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[InMale] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[InFemale] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[InReturnMoney] [int] NULL ,
[ReturnMoney] [int] NULL ,
[CustomType] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[Sales] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[Dress] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[DressExt] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[Prink] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[PrinkExt] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[PrinkMale] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[PrinkMaleExt] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[PrinkAssistant] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[PrinkAssistantExt] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[Photo] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[PhotoExt] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[PhotoProcExt] [int] NULL ,
[PhotoAssistant] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[PhotoAssistantExt] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[Selected] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[DigitalSelected] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[SelectProcExt] [int] NULL ,
[Design] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[DigitalDesign] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[Take] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
[PhotoEnd] [int] NULL ,
[PhotoExtEnd] [int] NULL ,
[SelectEnd] [int] NULL ,
[DesignEnd] [int] NULL ,
[DigitaSelectEnd] [int] NULL ,
[TakeEnd] [int] NULL ,
[Operator] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[Remark] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[P_Photo_Supply] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[P_Design_First_Accept] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[P_Design_First_Accept_Date] [datetime] NULL ,
[P_Design_First_Date] [datetime] NULL ,
[P_Photo_Amount] [int] NULL ,
[P_Design_First_EndDate] [datetime] NULL ,
[P_Select_Details] [nvarchar] (3000) COLLATE Chinese_PRC_CI_AS NULL ,
[P_Select_Amount] [int] NULL ,
[P_Select_Remark] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[P_DigitalSelect_Remark] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[ProcessEnd] [bit] NULL ,
[Custom_Type_Flag] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Money] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Money] [money] NOT NULL ,
[Shroff] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[Sales] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[Assistant] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[CustomNO] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[Date] [datetime] NOT NULL ,
[Type] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[PayType] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[CustomType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[CardNo] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[Remark] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[RobeID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Operator] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
GROUP BY a.CustomNO
的话,出来的结果是对的。但是加上sum(b.Price), 就不对了。
[其他解释]
SELECT Custom.CustomNO, Custom.Receivable AS Expr1, CustomDetails.Price AS Expr3,
SUM(Money.Money) AS Expr2
FROM Custom INNER JOIN
CustomDetails ON Custom.CustomNO = CustomDetails.CustomSN INNER JOIN
Money ON Custom.CustomNO = Money.CustomNO
WHERE (Custom.CustomNO = '0008950') AND (CustomDetails.Class = 'Retail')
GROUP BY Custom.CustomNO, Custom.Receivable, CustomDetails.Price