Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can optimizing select query in a Cartesian product of the same table? Is there any other way to solve my business scenario?
Am designing an application that assign container with specified weight and destination to cargo trucks with specified truck capacity to a specified destination.
My design of tables in SQL server 2008 looks the following:
1. I have a table named ‘tblTrucks’ that holds truck information like [TruckPlateNumber,TrailerNumber,Transporter,TruckCapacity,TruckType,RegistrationDate] Truck capacity values are 250,300,350,420,450,500,550,600,650 and Truck types values are two High Bed(can load one 40ft container or two 20ft containers) and Truck Trailer(Can load only two 20ft containers).
2. I have a table name ‘tblContainerInformation’ that holds container information like [ContainerNumber,ContainerSize,ContainerDestination,Weight,DischargeDate,CargoType,status].Container size values are 20 and 40. This table holds a maximum of 20,000 records at a time.
My application needs a Cartesian product of ‘tblContainerInformation’ to combine two different 20ft containers in order to assign for the trucks based on the capacity of the truck. What my application do is it will load the truck information based on their registration date and iterate every record to find two containers that meets the truck capacity and destination. When it finds it updates the container status to assigned.
When the Cartesian product is done it takes a lot of time. So many records; if you have 10,000 records you will have 100,000,000. It makes my application very slow. Besides the select query have to be sorted the combined discharge date. (Two Dates)
Therefore my question here is;
1. Is there a better way to solve the business scenario other than Cartesian product?
2. If not how can I optimize the query performance in the Cartesian product and make the query run faster?
Your help is appreciated. Thanks.
Mesfin Ayalew
Posted

1 solution

Quote: mesfinA
1. Is there a better way to solve the business scenario other than Cartesian product?
I would say yes, SQL is good at databases handling, but may not be suited to your problem.

Your problem look like a multi-constraint optimisation problem.
I would load the data in memory and make a specialised program in VB.NET

Quote:
2. If not how can I optimize the query performance in the Cartesian product and make the query run faster?
Need to see the query to answer :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900