开发者

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?


Do you have the appropriate indexes on the tables?

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


The query is as good as it can be. By their nature queries specify WHAT to do, not HOW to do it.

It's the RDMS below it that will effect your performance, and the main way to impact a query like this is to add indexes the columns on which you join (each table's oroductid)


The query is fine. Try indexing productid in both tables, as well as salestime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜