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?
JOINs andORDER BYs are where you will see a slow down, but if you changed those, you wouldn't get back the data you want, in the order you want it. The query looks good to me, but an index on thesalestimeandproduct_idcolumns would probably help speed things up a bit. Also, can you store this query as a view or stored procedure? When a SQL is called, the server needs to compile the SQL code, then execute it. When it's called from a view or stored proc, it's precompiled by the server, which could save a few more milliseconds..