make a dashboard to has to make similar query too many times and performance bad - postgresql

I have a question here. we have a customer list and product list and sale table. We want to show each customer to buy each product's total sale.
so I use the query like following:
select ...
from ...
where customer="" and product="".
the query is standard simple one. but the table/dashboard is 20*10. It means for each customer and product pair i have to run a query. i have to run query 200 times. which is super slow.
how to improve this? thanks
right now the dashboard give me 20 customer and 10 product and then i go to database for 200 times. it is from a customer list to pick first 20 and another 20 like this way. the product is the same way to choose.

You can use group by ... Something like
Select customer, product, sum(sales) -- or whatever you need
From ...
Group by customer, product
The server would do the aggregation, and the query is much faster than doing 200 queries

Related

Is there a better way to design an E-commerce products database when the products can have multiple price lines?

I'm trying to develop a new products database for a client and I am having a hard time finding a solution to handle the many different prices a product can have.
Variables that can affect the price of a product:
region (where the customer lives).
customer type (whether they're a new customer or a registered customer).
order type (whether you want to purchase the item once or subscribe monthly).
store type (there were many store types, but now they're transitioning to one, but I don't know if they will add more in the future again so I want this to be flexible
Product Table
id
product_name
sku
description
1
Vanilla Protein Powder
1111
This is a description...
Attributes Table
id
associate_type
region
store
currency_code
order_type
1
2
1
11
usd
1
2
2
1
11
usd
2
Product Attributes Table
id
price
product_id
attribute_id
1
49.95
1
1
2
29.95
1
2
I was initially thinking this would be a good route to go, but the issue that arises is on the product page I want to display the price of the product if you want to purchase it once and the price if you wanted to subscribe. The same issue would happen if I also wanted to show a list of product cards on a product category page displaying both prices.
The issue I run into
When I write a query to get the correct products with the correct price lines, I would filter all the products by the variables mentioned above, but I will always want the subscription price and the one-time price, so in the query, I would have to include WHERE order_type = 1 AND order_type = 2 but doing this would return duplicate records of the product/ products because of the different prices for a one-time purchase and a subscription purchase.
Is there a better way to set up the tables I have so that the query would not return duplicate records of the product/ products? Or is there a way to write the query to handle this for me? Or do I even need to switch up my database design altogether?

Crystal Reports Record Filtering using and

Currently I have a crystal report that only shows orders which included a "Storm Door" OR a "Sunroom Component". However I would like it to only show orders that include BOTH "Storm Door" AND "Sunroom Component" within a order. Any suggestions on how the code for this might look?
I think you have answered your own question - If you want the report to retrieve records where BOTH products were ordered, you would use an AND statement - {Categories.Description} = "Storm Doors" and {Categories.Description} = BetterView Sunroom Compnents". However, the issue with duplicate records sounds like a problem with the design of the report. You could try a few things
Look at the table joins and verify they are setup correctly; if you have a 'one to many' join setup, you may be retrieving multiple rows for each record. If you joined Customer to Orders on the Customer_ID, you would get every single order for each customer, etc.
Click Database > Select Distinct Records - Depending on how your report is configured and which fields you are displaying, this may remove the undesired duplicates.
Change your grouping order - Instead of grouping by Category (Storm door, Subroom), group by Customer and then count the number of orders or use an aggregate function to give you the information you need.

crystal reports total of values field

I'm using Crystal Reports 10 right now and I have my report almost finished. I have a group of customers and for each customer, I have multiple lines of customer information. The one thing I'm still trying to figure out though is how can I get a running total of one column's distinct values.
So, for example I have a customer Bob who has 6 different items. For each item, there is a manufacturer. It might be the same for all 6 items, it might be one for 3 of them and another for the other 3, or they might all be different. What I want is to have a field at the bottom of the group for Bob that would say:
Man1 - 3
Man2 - 1
Man3 - 2
with the manufacturer being on the left side and the number of items they make for that customer on the right side.
Is there anything in Crystal Reports that can do this right now? Or can someone give me some advice on where I would get started on a formula to do this? Thanks for any help you guys can give.
Need more information:
What kind of data structure are we talking about? How are the manufacturer and item data found? How are these related to the customer? How many manufacturers/items are typically found per customer?
If the manufacturer and items are two different fields, that might be simple (cross tab in customer group). If they are free form, that might be a problem. If there are separate fields for every manufacturer (I've see it happen), then that might be a problem.
Best scenario:
One field for manufacturer, one field for item, distinct relation to customer.
Possible solution:
Group by customer, sub-group by manufacturer, count of items.
Not best scenario:
Table for customer, separate table for each manufacturer.
Possible solution:
Main report, group by customer. Linked subreport on Manufacturer/items.
Worst scenario:
Free-text entry of manufacturer and items in a memo field.
Possible solution:
Redesign your database (grin).
If you have to, you can do this using logic tests through formulas, but if you have many manufacturers/items, it would get exhaustive.
Best answer: More information.

Database design challenge

I'm creating an virtual stamp card program for the iphone and have run into an issue with implementing my database. The program essentially has a main points system that can be utitlized through all merchants (sort've like air miles), but i also want to keep track of how many times you've been to EACH merchant
So far, i have created 3 main tables for users, merchants, and transactions.
1) Users table contains basic info like user_id and total points collected.
2) Merchants table contains info like merchant_id, location, total points given.
3) Transactions table simply creates a new row for every time someone checks into each merchant, and records date-stamp, user name, merchant name, and points awarded.
So the most basic way to deal with finding out how many times you've been to each merchant is to query the entire transaction table for both user and merchant, and this will give me a transaction history of how many times you've been to that specific merchant(which is perfect), but in the long run, i feel this will be horrible for performance.
The other straightforward, yet "dumb" method for implementing this, would be to create a column in the users table for EACH merchant, and keep the running totals there. This seems inappropriate, as I will be adding new merchants on a regular basis, and there would need to be new columns added to every user for every time this happens.
I've looked into one-to-many and many-to-many relationships for mySQL databases, but can't seem to come up with something very concrete, as i'm extremely new to web/PHP/mySQL development but i'm guessing this is what i'm looking for...
I've also thought of creating a special transaction table for each user, which will have a column for merchant and another for the # of times visited. Again, not sure if this is the most efficient implementation.
Can someone point me in the right direction?
You're doing the right thing in the sense of thinking up the different options, and weighing up the good and bad for each.
Personally, I'd go with a MerchantCounter table which joins on your Merchant table by id_merchant (for example) and which you keep up-to-date explicitly.
Over time it does not get slower (unlike an activity-search), and does not take up lots of space.
Edit: based on your comment, Janan, no I would use a single MerchantCounter table. So you've got your Merchant table:
id_merchant nm_merchant
12 Jim
15 Tom
17 Wilbur
You would add a single additional table, MerchantCounter (edited to show how to tally totals for individual users):
id_merchant id_user num_visits
12 101 3
12 102 8
15 101 6007
17 102 88
17 104 19
17 105 1
You can see how id_merchant links the table to the Merchant table, and id_user links to a further User table.

Faster CROSS JOIN alternative - PostgreSQL

I am trying to CROSS JOIN two tables, customers and items, so I can then create a sales by customer by item report. I have 2000 customer and 2000 items.
SELECT customer_name FROM customers; --Takes 100ms
SELECT item_number FROM items; --Takes 50ms
SELECT customer_name, item_number FROM customers CROSS JOIN items; Takes 200000ms
I know this is 4 million rows, but is it possible to get this to run any faster? I want to eventually join this with a sales table like this:
SELECT customer_name, item_number, sales_total FROM customers CROSS JOIN items LEFT JOIN sales ON (customer.customer_name = sales.customer_name, item.item_number=sales.item_number);
The sales table will obviously not have all customers or all items, so the goal here is to have a report that shows all customers and all items along with what was sold and not sold.
I'm using PostgreSQL 8.4
To answer your question: No, you can't do a cross join faster than that - if you could then that would be how CROSS JOIN would be implemented.
But really you don't want a cross join. You probably want two separate queries, one which lists all customers, and another which lists all items and whether or not they were sold.
This really needs to be multiple reports. I can think of several off the top of my head that will yield more efficient packaging of information:
Report: count of all purchases by customer/item (obvious).
Report: list of all items not purchased, by customer.
Report: Summary of Report #2 (count of items) in order to prioritize which customers to focus on.
Report: list of all customer that have not bought an item by item.
Report: Summary of Report #3 (count of customers) in order to identify both the most popular and unpopular items for further action.
Report: List of all customers who purchased an item in the past, but did not purchase it his reporting period. This report is only relevant when the sales table has a date and the customers are expected to be regular buyers (i.e. disposable widgets). Won't work as well for things like service contracts.
The point here is that one should not insist that the tool process every possible outcome at once and generate more data and anyone could possibly digest manually. One should engage the end-users and consumers of the data as to what their needs are and tailor the output to meet those needs. It will make both sides' lives much easier in the long run.
If you wish to see all items for a given client (even if the cient has no items), i would rather try
SELECT c.customer_name, i.item_number, s.sales_total
FROM customers c LEFT JOIN
sales s ON c.customer_name = s.customer_name LEFT OIN
items i on i.item_number=s.item_number
This should give you a list of all clients, and all items joined by sales.
Perhaps you want something like this?
select c.customer_name, i.item_number, count( s.customer_name ) as total_sales
from customers c full join sales s on s.customer_name = c.customer_name
full join items i on i.item_number = s.item_number
group by c.customer_name, i.item_number