Help with subquery

Discuss and learn how to use TableTracker for table selection and NoteTracker for taking automated notes.

Moderators: WhiteRider, kraada, Flag_Hippo, morny, Moderators

Help with subquery

Postby sawwee » Fri Jul 05, 2013 12:30 pm

I'm trying to create a custom stat which inludes a subquery.

My stat looks like:

SUM (CASE WHEN EXISTS
(SELECT 1 FROM
(SELECT (xxxxx) FROM tourney_hand_player_statistics thps_1 WHERE thps_1.id_player = 1 AND (xxxxx) ) as subtable WHERE subtable.xxxxxx AND subtable.id_hand = tourney_hand_player_statistics.id_hand)
THEN 1 ELSE 0 END)


(Sorry for deleting some part of my custom stats by xxxxxxx, frankly speaking I don't want to make my stat to be public.)

This stat is working perfectly until I want to get the result for the player who has id #1 in the database. But what should I do if I want to make this stat to work for any player (and put it to my HUD, for example?)
I tried to change ' WHERE thps_1.id_player = 1 ' to ' WHERE thps_1.id_player = tourney_hand_player_statistics.id_player ' , it seems to be giving a correct result, but running the quary takes a few HOURS.

What is the correct way to run a query including a subquery to ANY player?

Thanks a lot in advance!
sawwee
 
Posts: 513
Joined: Thu Dec 18, 2008 11:59 pm

Re: Help with subquery

Postby kraada » Fri Jul 05, 2013 2:26 pm

You could exclude times where subtable.id_player is the same as tourney_hand_player_statistics.id_player - I don't think you care about that case, and it should make the query actually finish a bit sooner as it'll eliminate this case -- and besides, you'd really never want to do a query like this for a stat when the player is the subquery also you could just do it directly then.
kraada
Moderator
 
Posts: 54431
Joined: Wed Mar 05, 2008 2:32 am
Location: NY

Re: Help with subquery

Postby sawwee » Fri Jul 05, 2013 5:43 pm

Thank you for your answer!

Frankly speaking I'm not perfectly understand what do you mean (especially not the last sentence you mention), however I tried to use this code instead of the original:

SUM (CASE WHEN EXISTS
(SELECT 1 FROM
(SELECT (xxxxx) FROM tourney_hand_player_statistics thps_1 WHERE thps_1.id_player = 1 AND (xxxxx) ) as subtable WHERE NOT (subtable.id_player = tourney_hand_player_statistics.id_player) AND subtable.xxxxxx AND subtable.id_hand = tourney_hand_player_statistics.id_hand)
THEN 1 ELSE 0 END)

It didn't really help, it takes still for hours, I definitely cannot use this stat in HUD.


What I actually need is to put the current player's id in the subquery. ("Where thps_1.id_player = ......" ) How can I do that? If I put any number there the query runs very fast and everything is nice. :-) But how can I tell to PT4 to put the id of the current player there?
sawwee
 
Posts: 513
Joined: Thu Dec 18, 2008 11:59 pm

Re: Help with subquery

Postby WhiteRider » Sat Jul 06, 2013 5:14 am

I'm not sure exactly what you're trying to do, but if you want information about the current player then you don't need a subquery - a normal query will automatically apply to the current player.
WhiteRider
Moderator
 
Posts: 54017
Joined: Sat Jan 19, 2008 7:06 pm
Location: UK

Re: Help with subquery

Postby BillGatesIII » Sat Jul 06, 2013 6:46 am

Try

Code: Select all
WHERE thps_1.id_player = player_real.id_player


but I think the problem is in the double SELECT. You might need one less to speed things up. Trying to figure out what you want to accomplish :mrgreen:
BillGatesIII
 
Posts: 740
Joined: Fri Dec 16, 2011 6:50 pm

Re: Help with subquery

Postby sawwee » Sat Jul 06, 2013 9:23 am

WhiteRider wrote:I'm not sure exactly what you're trying to do, but if you want information about the current player then you don't need a subquery - a normal query will automatically apply to the current player.


Unfortunately I definitely need a subquery, becuase there are some parts in it which can't be in the normal query.
sawwee
 
Posts: 513
Joined: Thu Dec 18, 2008 11:59 pm

Re: Help with subquery

Postby sawwee » Sat Jul 06, 2013 9:24 am

sawwee wrote:
WhiteRider wrote:I'm not sure exactly what you're trying to do, but if you want information about the current player then you don't need a subquery - a normal query will automatically apply to the current player.


Unfortunately I definitely need a subquery, becuase there are some parts in it which can't be in the normal query.



Thanks, I'll try it!
sawwee
 
Posts: 513
Joined: Thu Dec 18, 2008 11:59 pm

Re: Help with subquery

Postby sawwee » Sat Jul 06, 2013 9:39 am

sawwee wrote:
WhiteRider wrote:I'm not sure exactly what you're trying to do, but if you want information about the current player then you don't need a subquery - a normal query will automatically apply to the current player.


Unfortunately I definitely need a subquery, becuase there are some parts in it which can't be in the normal query.


Thank you BillGatesIII, it is much faster now, but still too slow to be able to use it in the HUD.

Isn't there any way to get the active player's id from "outside" of the query?
sawwee
 
Posts: 513
Joined: Thu Dec 18, 2008 11:59 pm

Re: Help with subquery

Postby BillGatesIII » Sat Jul 06, 2013 8:00 pm

player_real.id_player Is the active player from the outside query. It might be that the (xxxxx) part of your query does use non-indexed fields. If that's the case, it will be slow and you'll have to create indexes first.
BillGatesIII
 
Posts: 740
Joined: Fri Dec 16, 2011 6:50 pm

Re: Help with subquery

Postby sawwee » Sat Jul 06, 2013 8:05 pm

Hmmmmm.... But if I put the exact id instead of player_real.id_player (for example: thps_1.id_player = 1 ) then it works very quickly. What might be the reason?
sawwee
 
Posts: 513
Joined: Thu Dec 18, 2008 11:59 pm

Next

Return to TableTracker & NoteTracker

Who is online

Users browsing this forum: No registered users and 15 guests

cron