Solved

Faster alternative to a cross join.

Posted on 2007-03-29
8
2,169 Views
Last Modified: 2008-01-09
I would like to be able to show all CatID and ItemID from vwCatItemList for each ID in vwTest1 even if there is no checked value for the CatID and the ItemID in vwTest1.

I am currently using two views and a cross join to       acheive my results but the query is too slow.  I would like to be able to get the same results but without the cross join.
This is the results of vwCatItemList
                exp1                    CatID        ItemID
                Cat1Item1               1              1
      Cat1Item2      1      2
      Cat1Item3      1      3
      Cat1Item4      1      4
      Cat1Item5      1      5
      Cat1Item6      1      6
      Cat2Item8      2      8
      Cat2Item9      2      9
      Cat2Item10      2      10
      Cat2Item11      2      11
      Cat2Item12      2      12
      Cat2Item13      2      13
      Cat2Item14      2      14
      Cat2Item15      2      15
      Cat3Item16      3      16
      Cat3Item17      3      17
      Cat3Item18      3      18
      Cat3Item19      3      19
      Cat3Item20      3      20
      Cat3Item21      3      21
      Cat3Item22      3      22
      Cat4Item23      4      23
      Cat4Item24      4      24
      Cat4Item25      4      25
      Cat5Item26      5      26
      Cat5Item27      5      27
      Cat5Item28      5      28
      Cat5Item29      5      29
      Cat5Item30      5      30
      Cat6Item31      6      31
      Cat6Item32      6      32
      Cat6Item33      6      33

This is the results of vwTest1
                ID           CatID       ItemID    Checked
      5000033      4      25      -1
      5000035      4      24      -1
      5000041      2      8      -1
      5000041      3      20      -1
      5000041      5      30      -1
      5000042      2      8      -1
      5000042      2      13      -1

Desired output
                ID           CatID       ItemID      Checked
                5000041  1              1               0
                5000041  1              2               0
                5000041  1              3               0
                5000041  1              4               0
                5000041  1              5               0
                5000041  1              6               0
                5000041  2              1               0
                5000041  2              2               0
                5000041  2              3               0
                5000041  2              4               0                
                5000041  2              5               0
                5000041  2              6               0
                5000041  2              7               0
      5000041      2      8      -1
                5000041  2              9               0
                5000041  2              10             0
                5000041  2              11             0
                5000041  2              12             0
                5000041  2              13             0
                5000041  2              14             0
                5000041  2              15             0
                more records here
      5000041      3      20      -1
                more records here
      5000041      5      30      -1
                  
0
Comment
Question by:schlepuetz
  • 4
  • 3
8 Comments
 
LVL 9

Assisted Solution

by:dduser
dduser earned 100 total points
Comment Utility
Hi,
This is really impossible as there is no connection between vwtest1 and vwCatItemList. If you don't apply Cross Join your id would also come as null.

Select b.ID,a.CatId,a.ItemId,b.Checked from vwCatItemList Left Outer Join vwtest1 on a.CatId = b.CatId and a.ItemId = b.ItemId

Regards,

dduser
0
 
LVL 42

Accepted Solution

by:
dqmq earned 150 total points
Comment Utility
No matter how you spin it, you want a cross join, by definition. So the question really, is can you write a faster cross join than SQL.  Maybe.  But I think your time would be better spent optimizing for the SQL cross join.

Don't know what the views are doing, but given the output shown you should try to limit I/O to index scans--deally, indexes that are ordered to your advantage: ID, CatID, ItemID  
0
 
LVL 3

Author Comment

by:schlepuetz
Comment Utility
How can I limit the I/O to index scans?
0
 
LVL 42

Expert Comment

by:dqmq
Comment Utility
By having indexes that contain all the data needed by your query and composing the query so that it uses the indexes.  Start with an explain plan to see what you already get.  I don't mean to trivialize the optimization--it's art + knowledege + luck + persistence.

What indexes do you have?
What do your views look like?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Author Comment

by:schlepuetz
Comment Utility
Here is the two views and the tables that the views are built off of.


CREATE VIEW dbo.vwStopCatItemList
AS
SELECT     'Cat' + CAST(dbo.tblStopCategory.CategoryID AS nvarchar(2)) + 'Item' + CAST(dbo.tblStopItems.ItemID AS nvarchar(2)) AS Expr1,
                      dbo.tblStopCategory.CategoryID, dbo.tblStopItems.ItemID
FROM         dbo.tblStopCategory INNER JOIN
                      dbo.tblStopItems ON dbo.tblStopCategory.CategoryID = dbo.tblStopItems.CategoryID

CREATE VIEW dbo.vwJMSTStopListingCrossJoin
AS
SELECT     TOP 100 PERCENT dbo.tblJMSTStopObservation.StopID, dbo.vwStopCatItemList.Expr1
FROM         dbo.tblJMSTStopObservation CROSS JOIN
                      dbo.vwStopCatItemList

CREATE TABLE [dbo].[tblJMSTStopObservation] (
      [StopID] [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
      [PlantID] [int] NOT NULL ,
      [StopDate] [datetime] NOT NULL ,
      [Shift] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [ObserverID] [int] NOT NULL ,
      [AreaID] [int] NOT NULL ,
      [StopAllSafe] [bit] NOT NULL ,
      [CreatedBy] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [CreatedDate] [datetime] NULL ,
      [ModifiedBy] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [ModifiedDate] [datetime] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[tblJMSTStopObservation] WITH NOCHECK ADD
      CONSTRAINT [PK_tblJMSTStopObservation] PRIMARY KEY  CLUSTERED
      (
            [StopID]
      )  ON [PRIMARY]
GO

CREATE TABLE [dbo].[tblStopItems] (
      [ItemID] [int] IDENTITY (1, 1) NOT NULL ,
      [ItemDesc] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [CategoryID] [int] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[tblStopItems] WITH NOCHECK ADD
      CONSTRAINT [PK_tblStopItems] PRIMARY KEY  CLUSTERED
      (
            [ItemID]
      )  ON [PRIMARY]
GO

CREATE TABLE [dbo].[tblStopCategory] (
      [CategoryID] [int] IDENTITY (1, 1) NOT NULL ,
      [CategoryDesc] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[tblStopCategory] WITH NOCHECK ADD
      CONSTRAINT [PK_tblStopCategory] PRIMARY KEY  CLUSTERED
      (
            [CategoryID]
      )  ON [PRIMARY]
GO


exec sp_addextendedproperty N'MS_Description', N'List of main headings from Stop card', N'user', N'dbo', N'table', N'tblStopCategory'


GO
0
 
LVL 42

Expert Comment

by:dqmq
Comment Utility
Don't have much time now, but to start,  let's just simplify.  For the moment, forget the views and see how this performs:

SELECT O.StopID, I.CategoryID, I.ItemID
FROM         dbo.tblJMSTStopObservation O CROSS JOIN dbo.tblStopItems I

Then, add a unique index to tblStopItems with the columns CategoryID and ItemID and try again.  


0
 
LVL 3

Author Comment

by:schlepuetz
Comment Utility
CatID and ItmeID are from two seperate tables.  There is a view that is created to combine the Catid and the ItemID it is called vwStopCatItemList.  Then that view is cross joined with tblStopItems.
0
 
LVL 3

Author Comment

by:schlepuetz
Comment Utility
Ok so I have realized that doing a cross join with any large number of records is an exercise in futility.  38,000 rows cross joined with 30 rows = 1,140,000 (too many rows).  I have changed the front end of the application that was using the cross joins to be able to work without the view.  Thanks for the help, the suggestions and more importantly the nudge/push in a different direction.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now