Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql join query problem

I have 2 tables
1) "products" with fields (productid PK, name, description, price)
2) "sales" with fields (salesid PK, salestime, productid, customername, customeremail, status)

I need to display data in table format as

SalesID      Product Name      Amount      Customer Name      Customer Address      Payment Status

For this, I am using following query

SELECT s.salesid, p.name, p.price, s.customername, s.customeremail, s.status 
FROM sales s 
LEFT JOIN products p ON s.productid = p.productid 
ORDER BY salestime DESC 
LIMIT 0, 15 

Is there any way I can still optimize this query to run faster?

like image 815
I-M-JM Avatar asked Mar 28 '26 20:03

I-M-JM


1 Answers

Do you have the appropriate indexes on the tables?

Have a look at CREATE INDEX Syntax and How MySQL Uses Indexes

like image 51
Adriaan Stander Avatar answered Mar 31 '26 04:03

Adriaan Stander