php - How to find the difference between user balance and turnover more efficiently?
我想大声告诉你
我想大声告诉你 2017-05-16 13:12:08
0
1
588

Money in the user table is the user's balance, and s_money in the stream table is the flow generated by the user. Theoretically SUM(s_money) == money, assuming a failure occurs, how to find out the different data in the table? As shown in the figure below, the user with ID=3.

Supplement: You need to find out the information in the user table and the total turnover of the user

As

ID name money SUM(s_money)
3 z 150 100

Try to query the user and stream tables separately, and find the difference set through array_diff_assoc. But the efficiency is relatively low.

The measured user table has 23,434 pieces of data, and the stream table has 361,024 pieces of data. It takes a total of 3.5s-4s. Is there a more efficient method?


user table:

ID name money
1 L 50
2 W 100
3 Z 150

stream table:

ID userID s_money
1 1 -50
2 1 100
3 2 -10
4 2 110
5 3 -10
6 3 110

Currently, userID is indexed, and the performance has increased several times. The actual query time is within 0.3s.
The online environment has not been tested, so I don’t know if it can be used

我想大声告诉你
我想大声告诉你

reply all(1)
黄舟

select t1.id,t1.name,t1.money,t2.s_money from user t1,(select userID,sum(s_money) s_money from stream group by userID) t2 where t1.id=t2.userid;
Can establish a union The indexing effect will be better: create index INX_stream_us on stream (user_id,s_money);

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template