我想从一个视图中获取记录,但是在我的查询中还有一个视图保持连接状态,这导致获取记录的速度非常慢。
花5分钟时间才能提取13000条记录。
我的看法如下-
CREATE View [dbo].[VIEW_IQ_PURCHASE_ANALYSIS]
As
SELECT
ISNULL(MstOrganization.Name,'') as [Organization Name] ,
ISNULL(MstOrganization.Description,'') AS [Organization Description],
ISNULL(MstUser.Name,'') AS [User Name],
ItemLedger.DocumentNo as [Document No],
ItemLedger.DocumentDate AS [Document Date],
ItemLedger.FilterDocumentDate AS [From Date],
ItemLedger.FilterDocumentDate AS [To Date],
ItemLedger.DocumentYear as [Document Year],
ItemLedger.DocumentMonthName As [Document Month],
ISNULL(DocumentCurrency.Name,'') AS [Document Currency Name],
ISNULL(DocumentCurrency.Description,'') AS [Document Currency Description],
ItemLedger.DocumentExRate as [Document Exchange Rate],
ItemLedger.ReferenceNo AS [Reference No],
ItemLedger.ReferenceDate as [Reference Date],
CASE WHEN ItemLedger.VoucherTypeCode=2000 THEN
ISNULL(MstWalkinCustomer.Name,'')
ELSE ISNULL(MstBusinessPartner.Name,'') END AS [Party Name],
CASE WHEN ItemLedger.VoucherTypeCode=2000 THEN
ISNULL(MstWalkinCustomer.Description,'')
ELSE ISNULL(MstBusinessPartner.Description,'') END AS [Party Description],
ISNULL(BaseCurrency.Name,'') AS [Base Currency Name],
ISNULL(BaseCurrency.Description,'') AS [Base Currency Description],
ISNULL(BusinessPartnerAddressDetail.AddressTypeCode,0) AS [AddressType Code],
CASE WHEN ItemLedger.VoucherTypeCode=2000 THEN
ISNULL(WalkInCustomerAddressDetail.AddressType,'')
ELSE ISNULL(BusinessPartnerAddressType.Name,'') END AS [Address Type Name],
CASE WHEN ItemLedger.VoucherTypeCode=2000 THEN
ISNULL(WalkInCustomerAddressDetail.City,'')
ELSE ISNULL(BusinessPartnerCity.Name,'') END AS [City Name],
CASE WHEN ItemLedger.VoucherTypeCode=2000 THEN
ISNULL(WalkInCustomerAddressDetail.Country,'')
ELSE ISNULL(BusinessPartnerCountry.Name,'') END AS [Country Name],
CASE WHEN ItemLedger.VoucherTypeCode=2000 THEN
ISNULL(WalkInCustomerAddressDetail.State,'')
ELSE ISNULL(BusinessPartnerState.Name,'') END AS [State Name],
ISNULL(BusinessPartnerAddressDetail.AddressName,'') AS [Address Name],
CASE WHEN ItemLedger.VoucherTypeCode=2000 THEN
ISNULL(WalkInCustomerAddressDetail.Address,'')
ELSE ISNULL(BusinessPartnerAddressDetail.Address,'') END AS [Address],
CASE WHEN ItemLedger.VoucherTypeCode=2000 THEN
ISNULL(WalkInCustomerAddressDetail.ZipCode,'')
ELSE ISNULL(BusinessPartnerAddressDetail.ZipCode,'') END AS [Zip Code],
CASE WHEN ItemLedger.VoucherTypeCode=2000 THEN
ISNULL(WalkInCustomerAddressDetail.PhoneNo,'')
ELSE ISNULL(BusinessPartnerAddressDetail.PhoneNo,'') END AS [Phone No],
ItemLedger.PartyExRate AS [Party Exchange Rate],
ItemLedger.HeaderNotes AS [Notes],
ISNULL(mstShipper.Name,'') AS [Shipper Name],
ItemLedger.PriorityTypeName as [Priority Type],
ItemLedger.NoPackages AS [No Packages],
ItemLedger.DeliveryDate AS [Delivery Date],
ItemLedger.TrackingNo AS [Tracking No],
ISNULL(SalesmanBuyer.Name,'') AS [Buyer Name],
ISNULL(SalesmanBuyer.Description,'') AS [Buyer Description],
ISNULL(MstStockPoint.Name,'') AS [Stock Point Name],
ISNULL(MstStockPoint.Description,'') AS [Stock Point Description],
ISNULL(ItemUOM.Name,'') AS [UOM Name],
ISNULL(ItemUOM.Description,'') AS [UOM Description],
ISNULL(stmItem.Name,'') AS [Item Name],
ISNULL(stmItem.Description,'') AS [Item Description],
ItemLedger.LotNo AS [Lot No],
ItemLedger.ExpiryDate AS [Expiry Date],
ISNULL(mstProductCategory.Name,'') AS [Product Category Name],
ISNULL(mstProductCategory.Description,'') AS [Product Category Description],
ItemLedger.DetailDescription AS [Description],
ItemLedger.Quantity *
(CASE WHEN ItemLedger.InOut = 0
THEN 1
ELSE -1
END) As [Quantity],
ItemLedger.UOMQuantity *
(CASE WHEN ItemLedger.InOut = 0
THEN 1
ELSE -1
END) As [UOM Quantity],
ItemLedger.PriceStandardBC *
(CASE WHEN ItemLedger.InOut = 0
THEN 1
ELSE -1
END) As [Price],
ItemLedger.GrossAmountBC *
(CASE WHEN ItemLedger.InOut = 0
THEN 1
ELSE -1
END) As [Amount],
ItemLedger.PriceDiscountBC *
(CASE WHEN ItemLedger.InOut = 0
THEN 1
ELSE -1
END) As [Discount],
ItemLedger.DetailTaxAmountBC *
(CASE WHEN ItemLedger.InOut = 0
THEN 1
ELSE -1
END) AS [Tax Amount],
ItemLedger.NetAmountBC *
(CASE WHEN ItemLedger.InOut = 0
THEN 1
ELSE -1
END) As [Net Amount],
ItemLedger.TaxPercent AS [Tax Percent],
ISNULL(mstTax.Name,'') AS [Tax Name],
ISNULL(mstTax.Description,'') AS [Tax Description],
ItemLedger.DatePromised AS [Date Promised],
ItemLedger.DateDelivered AS [Date Delivered],
Characteristics.*
FROM VIEW_ITEM_STOCK_LEDGER as ItemLedger
LEFT JOIN **VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA** as Characteristics on Characteristics.Code = ItemLedger.ItemCode
LEFT JOIN MstGroup ON MstGroup.Code = ItemLedger.GroupCode
LEFT JOIN MstOrganization ON MstOrganization.Code = ItemLedger.OrganizationCode
LEFT JOIN MstCurrency As BaseCurrency On BaseCurrency.Code = MstOrganization.CurrencyCode
LEFT JOIN stmVoucherType ON stmVoucherType.Code = ItemLedger.VoucherTypeCode
LEFT JOIN MstCurrency As DocumentCurrency ON DocumentCurrency.Code = ItemLedger.DocumentCurrencyCode
LEFT JOIN MstUser ON MstUser.Code = ItemLedger.UserCode
LEFT JOIN MstBusinessPartner ON MstBusinessPartner.Code = ItemLedger.PartyCode
LEFT JOIN MstWalkinCustomer ON MstWalkinCustomer.Code = ItemLedger.PartyCode
LEFT JOIN MstAddressDetail As BusinessPartnerAddressDetail ON BusinessPartnerAddressDetail.ObjectCode = MstBusinessPartner.Code
AND BusinessPartnerAddressDetail.IsDefault = 1
LEFT JOIN MstWalkInCustomerAddressDetail As WalkInCustomerAddressDetail ON WalkInCustomerAddressDetail.ObjectCode = ItemLedger.PartyCode
AND WalkInCustomerAddressDetail.IsDefault = 1
LEFT JOIN MstAddressType As BusinessPartnerAddressType ON BusinessPartnerAddressType.Code = BusinessPartnerAddressDetail.AddressTypeCode
LEFT JOIN MstCity As BusinessPartnerCity ON BusinessPartnerCity.Code = BusinessPartnerAddressDetail.CityCode
LEFT JOIN MstCountry As BusinessPartnerCountry ON BusinessPartnerCountry.Code = BusinessPartnerAddressDetail.CountryCode
LEFT JOIN MstState As BusinessPartnerState ON BusinessPartnerState.Code = BusinessPartnerAddressDetail.StateCode
LEFT JOIN MstCurrency As BusinessPartnerCurrency ON BusinessPartnerCurrency.Code = MstBusinessPartner.CurrencyCode
LEFT JOIN MstUOM As DetailUOM ON DetailUOM.Code = ItemLedger.DetailUOMCode
LEFT JOIN stmItem ON stmItem.Code = ItemLedger.ItemCode
LEFT JOIN MstUOM As ItemUOM ON ItemUOM.Code = stmItem.UOMCode
LEFT JOIN mstProductCategory ON mstProductCategory.Code = stmItem.ProductCategoryCode
LEFT JOIN mstTax ON mstTax.Code = ItemLedger.TaxCode
LEFT JOIN MstStockPoint ON MstStockPoint.Code = ItemLedger.StockPointCode
LEFT JOIN MstBusinessPartner As SalesmanBuyer ON SalesmanBuyer.Code = ItemLedger.SalesmanBuyerCode
LEFT JOIN MstShipper ON MstShipper.Code = ItemLedger.ShipperCode
WHERE (ItemLedger.vouchertypecode=204 OR (ItemLedger.VoucherTypeCode=402 and ItemLedger.DocumentTypeCode=2))
我上方的视图中还有一个视图- VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA ,我将其与视图合并。
VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA 如下-
CREATE VIEW VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA
AS
SELECT mstProduct.Code,
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail WHERE
mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011786 )
As [Sub Category],
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail
WHERE mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011787 ) As [Brand],
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail
WHERE mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011788 ) As [Color],
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail
WHERE mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011789 ) As [Size],
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail
WHERE mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011790 ) As [Style],
(SELECT mstProductCharacteristicDetail.CharacteristicValue
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011791 ) As [Barcode],
(SELECT mstProductCharacteristicDetail.CharacteristicValue
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011792 ) As [Item Name Arabic]
FROM mstProduct
当我执行VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA时-它以非常快的速度(3秒)将记录获取给我,是什么原因导致无法加入该视图。
任何帮助将不胜感激。
答案 0 :(得分:2)
我在这里没有进行整个查询,读取起来很混乱而且很糟糕(抱歉,但是那些对象名称很大,而且没有别名,它已经完全膨胀了。)
我怀疑以下逻辑将对您的第一列Erlang/OTP 21 [erts-10.1] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> a = 1..10
1..10
iex(2)> is_list a
false
iex(3)> i a
Term
1..10
Data type
Range
Description
This is a struct. Structs are maps with a __struct__ key.
Reference modules
Range, Map
Implemented protocols
IEx.Info, Enumerable, Inspect
iex(4)>
起作用,因此您将能够应用相同的逻辑(只是更多[Sub Category]
表达式,没有更多子查询)来获取其余值:
CASE
注意:没有示例数据,这是完全未经测试的。