sCmd = "Select A.InventoryID, A.BarCode, A.InventoryName, A.Description, A.StockCode, A.Class, A.Unit, "
sCmd = sCmd & " A.Price, A.Price2, A.Price3, A.Price4, "
sCmd = sCmd & " A.NoPriceShift, A.AllowDecimalValue, A.Group1, A.Group2, A.Group3, A.Group4, A.Group5, "
sCmd = sCmd & " A.Active, B.CategoryNumber, A.PriceShiftID, "
sCmd = sCmd & " A.Flag1, B.Flag1 As DeptFlag1, A.Flag2, B.Flag2 As DeptFlag2 "
sCmd = sCmd & " From Inventory A INNER JOIN InventoryCategories B ON A.CategoryID = B.CategoryID "
sCmd = sCmd & " Where A.Class<>2 And A.Active "
Set oRs = oCmn.ExecRS(sCmd, DCN)
Do While Not oRs.EOF
Set oPL = New dPRODUCTLIST
oPL.InventoryID = oRs!InventoryID
oPL.BarCode = Trim(oRs!BarCode)
oPL.name = IIf(IsNull(oRs!InventoryName), "", Trim(oRs!InventoryName))
oPL.Description = IIf(IsNull(oRs!Description), "", Trim(oRs!Description))
oPL.StockCode = IIf(IsNull(oRs!StockCode), "", Trim(oRs!StockCode))
oPL.Class = IIf(IsNull(oRs!Class), 0, oRs!Class)
oPL.Unit = IIf(IsNull(oRs!Unit), "", Trim(oRs!Unit))
oPL.Price = IIf(IsNull(oRs!Price), 0, oRs!Price)
oPL.Price2 = IIf(IsNull(oRs!Price2), 0, oRs!Price2)
oPL.Price3 = IIf(IsNull(oRs!Price3), 0, oRs!Price3)
oPL.Price4 = IIf(IsNull(oRs!Price4), 0, oRs!Price4)
oPL.NoPriceShift = IIf(IsNull(oRs!NoPriceShift), False, oRs!NoPriceShift)
oPL.AllowDecimalValue = IIf(IsNull(oRs!AllowDecimalValue), False, oRs!AllowDecimalValue)
oPL.Group1 = IIf(IsNull(oRs!Group1), "", oRs!Group1)
oPL.Group2 = IIf(IsNull(oRs!Group2), "", oRs!Group2)
oPL.Group3 = IIf(IsNull(oRs!Group3), "", oRs!Group3)
oPL.Group4 = IIf(IsNull(oRs!Group4), "", oRs!Group4)
oPL.Group5 = IIf(IsNull(oRs!Group5), "", oRs!Group5)
oPL.CategoryNumber = IIf(IsNull(oRs!CategoryNumber), "", oRs!CategoryNumber)
oPL.PriceShiftID = IIf(IsNull(oRs!PriceShiftID), 0, oRs!PriceShiftID)
If Left(oRs!Flag1, 1) = "1" Then
oPL.TaxFlag = Right(oRs!Flag1, 6)
Else
oPL.TaxFlag = Right(oRs!DeptFlag1, 6)
End If
If Left(oRs!Flag2, 1) = "1" Then
oPL.PrinterFlag = Right(oRs!Flag2, 7)
Else
oPL.PrinterFlag = Right(oRs!DeptFlag2, 7)
End If
dPRODCOLL.Add CStr(UCase(Trim(oPL.BarCode))), oPL
oRs.MoveNext
Loop
mysql> explain inventory;
+-------------------+---------------+------+-----+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------+------+-----+----------+-------+
| InventoryID | int(11) | NO | PRI | 0 | |
| BarCode | varchar(16) | NO | UNI | NULL | |
| InventoryName | varchar(16) | NO | | NULL | |
| Description | varchar(50) | YES | | NULL | |
| CategoryID | int(11) | NO | MUL | 0 | |
| Class | int(11) | NO | MUL | 0 | |
| Unit | varchar(10) | NO | | PCS | |
| Price | decimal(19,4) | YES | | 0.0000 | |
| Price2 | decimal(19,4) | YES | | 0.0000 | |
| Price3 | decimal(19,4) | YES | | 0.0000 | |
| Price4 | decimal(19,4) | YES | | 0.0000 | |
| Onhand | double | YES | | 0 | |
| ReorderLevel | double | YES | | 0 | |
| MaxiumLevel | double | YES | | 0 | |
| SafetyLevel | double | YES | | 0 | |
| DateCreated | datetime | YES | | NULL | |
| Group1 | varchar(16) | YES | MUL | NULL | |
| Group2 | varchar(16) | YES | MUL | NULL | |
| Group3 | varchar(16) | YES | MUL | NULL | |
| Group4 | varchar(16) | YES | MUL | NULL | |
| Group5 | varchar(16) | YES | MUL | NULL | |
| NoPriceShift | tinyint(4) | YES | | 0 | |
| Active | tinyint(4) | YES | | 0 | |
| Updated | tinyint(4) | YES | | 0 | |
| Photograph | longblob | YES | | NULL | |
| Notes | text | YES | | NULL | |
| BranchUpdated | tinyint(4) | YES | | 0 | |
| StockCode | varchar(16) | YES | | NULL | |
| Flag1 | varchar(8) | YES | | 00000000 | |
| Flag2 | varchar(8) | YES | | 00000000 | |
| PriceShiftId | int(11) | NO | | 0 | |
| AllowDecimalValue | tinyint(4) | YES | | 0 | |
+-------------------+---------------+------+-----+----------+-------+
32 rows in set (0.00 sec)当IVENTORYID>5W的时候,系统需要20-30秒才能进,看看还需要什么优化?
sCmd = sCmd & " A.Price, A.Price2, A.Price3, A.Price4, "
sCmd = sCmd & " A.NoPriceShift, A.AllowDecimalValue, A.Group1, A.Group2, A.Group3, A.Group4, A.Group5, "
sCmd = sCmd & " A.Active, B.CategoryNumber, A.PriceShiftID, "
sCmd = sCmd & " A.Flag1, B.Flag1 As DeptFlag1, A.Flag2, B.Flag2 As DeptFlag2 "
sCmd = sCmd & " From Inventory A INNER JOIN InventoryCategories B ON A.CategoryID = B.CategoryID "
sCmd = sCmd & " Where A.Class<>2 And A.Active "
Set oRs = oCmn.ExecRS(sCmd, DCN)
Do While Not oRs.EOF
Set oPL = New dPRODUCTLIST
oPL.InventoryID = oRs!InventoryID
oPL.BarCode = Trim(oRs!BarCode)
oPL.name = IIf(IsNull(oRs!InventoryName), "", Trim(oRs!InventoryName))
oPL.Description = IIf(IsNull(oRs!Description), "", Trim(oRs!Description))
oPL.StockCode = IIf(IsNull(oRs!StockCode), "", Trim(oRs!StockCode))
oPL.Class = IIf(IsNull(oRs!Class), 0, oRs!Class)
oPL.Unit = IIf(IsNull(oRs!Unit), "", Trim(oRs!Unit))
oPL.Price = IIf(IsNull(oRs!Price), 0, oRs!Price)
oPL.Price2 = IIf(IsNull(oRs!Price2), 0, oRs!Price2)
oPL.Price3 = IIf(IsNull(oRs!Price3), 0, oRs!Price3)
oPL.Price4 = IIf(IsNull(oRs!Price4), 0, oRs!Price4)
oPL.NoPriceShift = IIf(IsNull(oRs!NoPriceShift), False, oRs!NoPriceShift)
oPL.AllowDecimalValue = IIf(IsNull(oRs!AllowDecimalValue), False, oRs!AllowDecimalValue)
oPL.Group1 = IIf(IsNull(oRs!Group1), "", oRs!Group1)
oPL.Group2 = IIf(IsNull(oRs!Group2), "", oRs!Group2)
oPL.Group3 = IIf(IsNull(oRs!Group3), "", oRs!Group3)
oPL.Group4 = IIf(IsNull(oRs!Group4), "", oRs!Group4)
oPL.Group5 = IIf(IsNull(oRs!Group5), "", oRs!Group5)
oPL.CategoryNumber = IIf(IsNull(oRs!CategoryNumber), "", oRs!CategoryNumber)
oPL.PriceShiftID = IIf(IsNull(oRs!PriceShiftID), 0, oRs!PriceShiftID)
If Left(oRs!Flag1, 1) = "1" Then
oPL.TaxFlag = Right(oRs!Flag1, 6)
Else
oPL.TaxFlag = Right(oRs!DeptFlag1, 6)
End If
If Left(oRs!Flag2, 1) = "1" Then
oPL.PrinterFlag = Right(oRs!Flag2, 7)
Else
oPL.PrinterFlag = Right(oRs!DeptFlag2, 7)
End If
dPRODCOLL.Add CStr(UCase(Trim(oPL.BarCode))), oPL
oRs.MoveNext
Loop
mysql> explain inventory;
+-------------------+---------------+------+-----+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------+------+-----+----------+-------+
| InventoryID | int(11) | NO | PRI | 0 | |
| BarCode | varchar(16) | NO | UNI | NULL | |
| InventoryName | varchar(16) | NO | | NULL | |
| Description | varchar(50) | YES | | NULL | |
| CategoryID | int(11) | NO | MUL | 0 | |
| Class | int(11) | NO | MUL | 0 | |
| Unit | varchar(10) | NO | | PCS | |
| Price | decimal(19,4) | YES | | 0.0000 | |
| Price2 | decimal(19,4) | YES | | 0.0000 | |
| Price3 | decimal(19,4) | YES | | 0.0000 | |
| Price4 | decimal(19,4) | YES | | 0.0000 | |
| Onhand | double | YES | | 0 | |
| ReorderLevel | double | YES | | 0 | |
| MaxiumLevel | double | YES | | 0 | |
| SafetyLevel | double | YES | | 0 | |
| DateCreated | datetime | YES | | NULL | |
| Group1 | varchar(16) | YES | MUL | NULL | |
| Group2 | varchar(16) | YES | MUL | NULL | |
| Group3 | varchar(16) | YES | MUL | NULL | |
| Group4 | varchar(16) | YES | MUL | NULL | |
| Group5 | varchar(16) | YES | MUL | NULL | |
| NoPriceShift | tinyint(4) | YES | | 0 | |
| Active | tinyint(4) | YES | | 0 | |
| Updated | tinyint(4) | YES | | 0 | |
| Photograph | longblob | YES | | NULL | |
| Notes | text | YES | | NULL | |
| BranchUpdated | tinyint(4) | YES | | 0 | |
| StockCode | varchar(16) | YES | | NULL | |
| Flag1 | varchar(8) | YES | | 00000000 | |
| Flag2 | varchar(8) | YES | | 00000000 | |
| PriceShiftId | int(11) | NO | | 0 | |
| AllowDecimalValue | tinyint(4) | YES | | 0 | |
+-------------------+---------------+------+-----+----------+-------+
32 rows in set (0.00 sec)当IVENTORYID>5W的时候,系统需要20-30秒才能进,看看还需要什么优化?
show create table InventoryCategories ;
show index from InventoryCategories ;
show index from Inventory ;
explian Select A.InventoryID, A.BarCode, A.InventoryNa.........
mysql> show create table inventory;
+-----------+-------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
---------------------+
| Table | Create Table |+-----------+-------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
---------------------+
| inventory | CREATE TABLE `inventory` (
`InventoryID` int(11) NOT NULL default '0',
`BarCode` varchar(16) NOT NULL,
`InventoryName` varchar(16) NOT NULL,
`Description` varchar(50) default NULL,
`CategoryID` int(11) NOT NULL default '0',
`Class` int(11) NOT NULL default '0',
`Unit` varchar(10) NOT NULL default 'PCS',
`Price` decimal(19,4) default '0.0000',
`Price2` decimal(19,4) default '0.0000',
`Price3` decimal(19,4) default '0.0000',
`Price4` decimal(19,4) default '0.0000',
`Onhand` double default '0',
`ReorderLevel` double default '0',
`MaxiumLevel` double default '0',
`SafetyLevel` double default '0',
`DateCreated` datetime default NULL,
`Group1` varchar(16) default NULL,
`Group2` varchar(16) default NULL,
`Group3` varchar(16) default NULL,
`Group4` varchar(16) default NULL,
`Group5` varchar(16) default NULL,
`NoPriceShift` tinyint(4) default '0',
`Active` tinyint(4) default '0',
`Updated` tinyint(4) default '0',
`Photograph` longblob,
`Notes` text,
`BranchUpdated` tinyint(4) default '0',
`StockCode` varchar(16) default NULL,
`Flag1` varchar(8) default '00000000',
`Flag2` varchar(8) default '00000000',
`PriceShiftId` int(11) NOT NULL default '0',
`AllowDecimalValue` tinyint(4) default '0',
PRIMARY KEY (`InventoryID`),
UNIQUE KEY `BarCode` (`BarCode`),
UNIQUE KEY `InventoryID` (`InventoryID`),
KEY `Class` (`Class`),
KEY `CategoryID` (`CategoryID`),
KEY `Group1` (`Group1`),
KEY `Group2` (`Group2`),
KEY `Group3` (`Group3`),
KEY `Group4` (`Group4`),
KEY `Group5` (`Group5`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------+-------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
---------------------+
1 row in set (0.00 sec)mysql>
mysql> show create table InventoryCategories;
| InventoryCategories | CREATE TABLE `inventorycategories` (
`CategoryID` int(11) NOT NULL,
`CategoryNumber` varchar(4) NOT NULL default '0',
`CategoryName` varchar(16) NOT NULL,
`Description` varchar(50) default NULL,
`DateCreated` datetime default NULL,
`Active` tinyint(4) default '0',
`Updated` tinyint(4) default '0',
`BranchUpdated` tinyint(4) default '0',
`Flag1` varchar(8) default '00000000',
`Flag2` varchar(8) default '00000000',
PRIMARY KEY (`CategoryID`),
UNIQUE KEY `CategoryID` (`CategoryID`),
UNIQUE KEY `CategoryNumber` (`CategoryNumber`),
UNIQUE KEY `CategoryName` (`CategoryName`),
KEY `Description` (`Description`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
1 row in set (0.06 sec)
mysql> show index from Inventory;
+-----------+------------+-------------+--------------+-------------+-----------
+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation
| Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-----------+------------+-------------+--------------+-------------+-----------
+-------------+----------+--------+------+------------+---------+
| Inventory | 0 | PRIMARY | 1 | InventoryID | A
| 121 | NULL | NULL | | BTREE | |
| Inventory | 0 | BarCode | 1 | BarCode | A
| 121 | NULL | NULL | | BTREE | |
| Inventory | 0 | InventoryID | 1 | InventoryID | A
| 121 | NULL | NULL | | BTREE | |
| Inventory | 1 | Class | 1 | Class | A
| 2 | NULL | NULL | | BTREE | |
| Inventory | 1 | CategoryID | 1 | CategoryID | A
| 11 | NULL | NULL | | BTREE | |
| Inventory | 1 | Group1 | 1 | Group1 | A
| 1 | NULL | NULL | YES | BTREE | |
| Inventory | 1 | Group2 | 1 | Group2 | A
| 1 | NULL | NULL | YES | BTREE | |
| Inventory | 1 | Group3 | 1 | Group3 | A
| 1 | NULL | NULL | YES | BTREE | |
| Inventory | 1 | Group4 | 1 | Group4 | A
| 1 | NULL | NULL | YES | BTREE | |
| Inventory | 1 | Group5 | 1 | Group5 | A
| 1 | NULL | NULL | YES | BTREE | |
+-----------+------------+-------------+--------------+-------------+-----------
+-------------+----------+--------+------+------------+---------+
10 rows in set (0.00 sec)
mysql> show index from InventoryCategories ;
+---------------------+------------+----------------+--------------+------------
----+-----------+-------------+----------+--------+------+------------+---------
+
| Table | Non_unique | Key_name | Seq_in_index | Column_name
| Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment
|
+---------------------+------------+----------------+--------------+------------
----+-----------+-------------+----------+--------+------+------------+---------
+
| InventoryCategories | 0 | PRIMARY | 1 | CategoryID
| A | 11 | NULL | NULL | | BTREE |
|
| InventoryCategories | 0 | CategoryID | 1 | CategoryID
| A | 11 | NULL | NULL | | BTREE |
|
| InventoryCategories | 0 | CategoryNumber | 1 | CategoryNum
ber | A | 11 | NULL | NULL | | BTREE |
|
| InventoryCategories | 0 | CategoryName | 1 | CategoryNam
e | A | 11 | NULL | NULL | | BTREE |
|
| InventoryCategories | 1 | Description | 1 | Description
| A | 11 | NULL | NULL | YES | BTREE |
|
+---------------------+------------+----------------+--------------+------------
----+-----------+-------------+----------+--------+------+------------+---------
+
5 rows in set (0.02 sec)mysql> explain Select A.InventoryID, A.BarCode, A.InventoryName, A.Description,
A.StockCode, A.Class, A.Unit, A.Price, A.Price2, A.Price3, A.Price4, A.NoPrice
Shift, A.AllowDecimalValue, A.Group1, A.Group2, A.Group3, A.Group4, A.Group5, A
.Active, B.CategoryNumber, A.PriceShiftID, A.Flag1, B.Flag1 As DeptFlag1, A.Fla
g2, B.Flag2 As DeptFlag2 From Inventory A INNER JOIN InventoryCategories B ON A
.CategoryID = B.CategoryID Where A.Class<>2 And A.Active
-> ;
+----+-------------+-------+------+--------------------+------------+---------+-
----------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len |
ref | rows | Extra |
+----+-------------+-------+------+--------------------+------------+---------+-
----------------+------+-------------+
| 1 | SIMPLE | B | ALL | PRIMARY,CategoryID | NULL | NULL |
NULL | 11 | |
| 1 | SIMPLE | A | ref | Class,CategoryID | CategoryID | 4 |
c1.B.CategoryID | 11 | Using where |
+----+-------------+-------+------+--------------------+------------+---------+-
----------------+------+-------------+
2 rows in set (0.00 sec)mysql>mysql>
A.StockCode, A.Class, A.Unit, A.Price, A.Price2, A.Price3, A.Price4, A.NoPrice
Shift, A.AllowDecimalValue, A.Group1, A.Group2, A.Group3, A.Group4, A.Group5, A
.Active, B.CategoryNumber, A.PriceShiftID, A.Flag1, B.Flag1 As DeptFlag1, A.Fla
g2, B.Flag2 As DeptFlag2 From Inventory A INNER JOIN InventoryCategories B ON A
.CategoryID = B.CategoryID Where A.Class<>2 And A.Active
在MYSQL中直接运行需要多久?应该不超过1秒。直接测试一下,如果MYSQL中速度不慢的话,你需要优化的就不是MYSQL表了,而你的VB代码。
这个DATA是数据比较少,运行的快,当数据超过5W的时候就比较慢了。
程序里面是把所有的数据COLLECTION一次,所以就比较慢了。
oPL.name = Trim(oRs!InventoryName) & vbnullstring
最好trim也不要用,这个应该是数据写入时处理的
慢?还是循环慢?(你查询出来数据记录条数很多吗?)
Do循环内,每一次循环都要Set oPL=New dPRODUCTLIST,对象没有释放。能否把Set oPL = New dPRODUCTLIST放在Do外面,循环结束后,释放对象。放在DO内也是可以,在LOOP前面加上一句Set oPL=Nothing。
要知道,iif函数一定是要测试二个条件的,而if ... then ... end 语句只要满足了条件,就不会测试else以下的语句,所以在10w数据下建议你用if语句....
这样isnull也省了...不过和if语句比较谁更快,你要测试,因为串接字符串也不快,10w数据不好说...
'简单测试
Dim a As Long, b As Longa = 15: b = 0If a > 10 Then
Debug.Print a
Else
Debug.Print a / b
End If
'以上不会执行else语句'以下满足a>10,但还会测试a<=10的情况下的表达式,b=0所以报错
Debug.Print IIf(a > 10, a, a / b)
而是出现是另外一张表CUSTOMERS,只有将近7000条数据,居然COLLECT了25秒,情何以堪呀!!!
谢谢 vbman2003
1.不要一次数据都取出来,根据需要取
2.取出来后不要扔到collection里面,保留在recordset随用随分析
3.Set oRs = oCmn.ExecRS(sCmd, DCN,1,1)
试试看