|
I have an incredibly irritating problem, in that this seems like it should be so easy but I just cant fathom it. Basically, I have a column, birth_date, and I want to get the youngest one for each year. So far, I've not had much luck, mostly because I suck, but this is my general idea: code:
|
# ? Sep 19, 2008 20:36 |
|
|
# ? May 15, 2024 07:55 |
|
What version of SQL are you using? This problem is ripe for analytical functions if your version supports them. vvvv I just wanted to use the fancy min ... over capabilities Aredna fucked around with this message at 00:51 on Sep 20, 2008 |
# ? Sep 19, 2008 21:09 |
|
I'm using Oracle, also I don't know what those are, so I shall go and investigate!
|
# ? Sep 19, 2008 22:31 |
|
No analytics needed:code:
|
# ? Sep 19, 2008 23:28 |
|
Stephen posted:I'm creating a views table that tracks which items on my site were viewed/clicked etc. and what time. The table has become far too large to efficiently query and results, so I'm creating a view count column in the item table to do efficient sorts by number of views. I guess my real question is about Triggers. Will it be viable/good practice to create a MySQL Trigger that will update the item view_count column on every insert into the views table? Or is there a better way to accomplish this? It's certainly possible to create a trigger. Another way is to create an SQL stored procedure that inserts into the views table and then increments view_count in the same transaction. Then call that procedure instead of directly inserting into the views table. I don't know which way is better, though. My co-workers say to avoid triggers in general because they can complicate things. But that might be because we're using an object-relational thing over the database. If you're just using plain SQL, I'm not sure.
|
# ? Sep 20, 2008 02:17 |
|
MoNsTeR posted:No analytics needed: Oh amazing, thankyou!
|
# ? Sep 20, 2008 03:01 |
|
I need to come up with a query in mysql 5: I've got a table called tblvotes with fields: voteID, storyID and genre. I need to query the table to return a storyID field containing a list of unique storyID's and another column containing the genre that occurs the most number of times for the corresponding storyID I realize this is probably very simple, but I am new to this. Can someone help?
|
# ? Sep 21, 2008 12:02 |
|
smith7800 posted:I've got a table called tblvotes with fields: voteID, storyID and genre. code:
|
# ? Sep 21, 2008 17:07 |
|
Pento McGreno posted:
Thanks so much. Worked perfectly.
|
# ? Sep 21, 2008 22:46 |
|
I'd like to ask here to see if any of you guys have any idea on what I might be able to do to speed up an operation I'm working on. In SQL 2005, I've got a table with a large amount of data, millions of records. The table has a primary key that's a single ID column that's a uniqueidentifier. These ID attributes have been filled, to this point, using newid(). The data I'm really interested in, though, is the data inside an untyped XML column. The column I have has data something like this: code:
code:
Now, that's not exactly what I have(I'm running the operation in batches, I'm doing a lookup on two other columns that aren't nearly as significant, and I just rewrote that xquery off the top of my head), but that's the gist of it. I've got an index on the Date column, so that's not really the big issue. The query as it stands now is just entirely too slow. I'm batching in groups of 10,000 records, and removing 1 batch of 10,000 records takes about 15 minutes to run. This isn't really acceptable, because this bit of code is going to have to run, alongside several other routine maintenance and cleanup operations inside of a short window of scheduled downtime. Outside of that window, that table requires high availability and I can't be tying it up trying to white out large numbers of rows. I've placed an XML Index on the table to try to speed up my .exists() function, but in order to get that XML index on the table, I had to change the existing primary key index from a non-clustered index to a clustered one(since it's required for the XML index), and I've been reading about how clustered indexes on GUID columns are a bad idea if the GUIDs have been created using newid(). I can move to where we'll begin utilizing newsequentialid(), but it'll be several years before the old random data is all gone. However, if I'm going to have an XML index, which seems like it's going to be necessary for any sort of decent speed, I'm going to need to have this. I'm out of ideas for what I can do to speed this up. It's unacceptably slow now, but if it's as fast as it's going to get without significantly restructuring the table somehow, then it's all I can do. I'm just hoping that there's some avenue here that I've missed that I can use to increase performance. I've got some options for potentially doing different behavior in the hopes that I could do something less costly, but I'd like to be sure first that I haven't missed an avenue for increasing performance while doing this is the right way, especially since all of those other options get into having to do a system impact to see how other bits of the application riding on this database would react. Any thoughts?
|
# ? Sep 22, 2008 07:44 |
|
Argh this query is killing me... Basically I want to find the 10 most popular scenes (a line item == a purchase of a scene): SELECT scenes.*, count(*) as popularity FROM "scenes" JOIN line_items ON line_items.scene_id = scenes.id GROUP BY line_items.scene_id ORDER BY popularity DESC LIMIT 10 I'm getting this error in postgres though (works perfectly in my local sqlite development db): ERROR: column "scenes.id" must appear in the GROUP BY clause or be used in an aggregate function Jargon fucked around with this message at 10:01 on Sep 22, 2008 |
# ? Sep 22, 2008 09:57 |
|
thefncrow posted:Stuff Make a stored function that you pass the XML data. That function will then check the XML string to see if it has the section you want to remove, then it will remove it and return the new XML, otherwise it will return the old XML, so your new query would be something like this. code:
You could probably also do this with an IIF or CASE and that would keep it from updating every single row. In that solution you would write two functions, one to see if the xml column exists that returns boolean, and a second that returns the modified xml.
|
# ? Sep 22, 2008 13:09 |
|
Jargon posted:Argh this query is killing me... Basically I want to find the 10 most popular scenes (a line item == a purchase of a scene): Try removing line_items.scene_id and replacing with scenes.id, then switch scenes.* to scenes.id in your select clause and see what happens. You should look at the GROUP BY clause documentation for postgres. That clause works different depending on the distribution and sometimes every single column that is selected must be included in the group by clause, sometimes not.
|
# ? Sep 22, 2008 13:17 |
|
Begby posted:Try removing line_items.scene_id and replacing with scenes.id, then switch scenes.* to scenes.id in your select clause and see what happens. Ok this worked: SELECT scenes.id, count(*) as popularity FROM "scenes" JOIN line_items ON line_items.scene_id = scenes.id GROUP BY scenes.id ORDER BY popularity DESC LIMIT 10 It returned a list of scene IDs and the popularity. However, I'd like to return scenes.* if possible since I have to get that information anyway and I'd like to avoid doing two queries. I tried this: SELECT scenes.*, count(*) as popularity FROM "scenes" JOIN line_items ON line_items.scene_id = scenes.id GROUP BY scenes.id ORDER BY popularity DESC LIMIT 10 And I got this error: ERROR: column "scenes.filename_base" must appear in the GROUP BY clause or be used in an aggregate function So it does seem like every field has to be either in GROUP by or an aggregate clause. I'm not really sure how I would do that though.
|
# ? Sep 22, 2008 17:24 |
|
Jargon posted:Ok this worked: All you have to do is GROUP BY scenes.id, scenes.otherField, scenes.otherField2, scenes.otherField3 etc... You can probably select the scene id and count as a subquery then join that to the rest of the fields as well. I am not all that familiar with postgres though, you should look at the group by documentation, I am sure there is a solution if you really don't want to have to list all the fields.
|
# ? Sep 22, 2008 17:35 |
|
Jargon posted:Ok this worked: I don't know postgres, and I'm coming at this from a SQL 2005 background, so I can't claim to know that this works, but it's what I'd suggest. Do the group by on the line_items table only, and then LEFT OUTER JOIN scenes to your grouped line_items table. Something like code:
I know SQL Server is also picky like this, where a group by query requires that all columns in the select list be either in the group statement, or in an aggregate. Your other option here, if the "grouping" is really only on 1 item like it seems you have here(as I'm assuming you don't have multiple Scenes with the same ID), you can defeat this by using the aggregate functions, like, say, selecting MAX(scenes.filename_base), which will work, because it's an aggregate function, and there's only 1 value for filename_base that's repeated several times in the join, so it's also MAX(filename_base). Again, though, I haven't used Postgres, so my suggestions are probably best taken with a strong dose of documentation. thefncrow fucked around with this message at 18:24 on Sep 22, 2008 |
# ? Sep 22, 2008 17:46 |
|
Is it possible to sort/select by a value given by a function? I've got a table with customers (students) and their Dutch bank account numbers, which can be validated with this function: code:
code:
I could load the whole table into PHP arrays first, and filter afterwards, but that is very slow and inconvenient...
|
# ? Sep 22, 2008 20:12 |
|
Mo Hawk posted:Is it possible to sort/select by a value given by a function? code:
|
# ? Sep 22, 2008 20:16 |
|
Jethro posted:I don't guarantee my syntax, but Thanks, I was looking for something just like that. Quite sadly I just discovered that the Function did not work as expected. I use this code:
code:
|
# ? Sep 23, 2008 00:11 |
|
SQL Server 2005... I have a query, that is far too long to paste here. Basically we have a large content database and I wrote a query to audit on all the potential issues we have with content on a daily basis... The Title equals the Description... odd characters finding their way into the content etc... One of the items I have in there is a hits count based on a summary table with about 3,000,000 rows give or take a million. The hits table has an index on the date. As a query this all completes in about 20 minutes. Using the exact same query a stored procedure version takes over 5 hours before I stopped it because it was annoying me. When the stored procedure executes it chooses NOT to use the index on my hits table. I've tried recompiling and giving the procedure table hints but it doesn't seem to matter. Any ideas? This sucks. benisntfunny fucked around with this message at 20:28 on Sep 23, 2008 |
# ? Sep 23, 2008 20:26 |
|
I'm trying to create a mySQL trigger that'll zero out some of the data for that row when the value of the status field for the row has changed. I've got the following code, which the server accepts and creates a trigger. However, it's not functioning properly, and I can't find any decent references for trigger syntax. Can anyone shed light on why this isn't working properly?code:
Roseo fucked around with this message at 21:50 on Sep 23, 2008 |
# ? Sep 23, 2008 21:41 |
|
Im trying to create a table in ORACLE that has a list of location descriptions as the rows, and then as the columns a breakdown of boundaries that information can cost, < 1, between one and 2.99, etc. What I want to do is, tally up the number of times that a target in a given location generates a cost of that boundary. So for example, if in Dartmouth three targets generated a cost of 0.4, 2 and 2.5 I would have a one in the first column, and a two in the second column. Also I only care about locations that have more than 3 targets in them. Heres what I have so far. code:
!Klams fucked around with this message at 18:33 on Sep 24, 2008 |
# ? Sep 24, 2008 18:31 |
|
!Klams posted:At the moment the trouble I'm having is that it doesn't like the way I've formed my DECODE statements, claiming its 'missing an expression' so I cant even get that far. Any ideas? I don't have any advice on the joins. Use CASE instead of decode here. code:
|
# ? Sep 24, 2008 19:06 |
|
var1ety posted:I don't have any advice on the joins. Use CASE instead of decode here. Ok awesome, thankyou! for whatever reason CASE works when DECODE doesn't. I am having the problem I thought I would though, gonna have to wrap my noggin round some bizzarre inner joins.
|
# ? Sep 24, 2008 19:32 |
|
I need to combine the results of two select statements into a single one. Both select statements select identical data. I tried using the UNION command but it results in a distinct type result set and I want to keep the repeated results. Is that possible? edit: is UNION ALL what I'm looking for? MrHyde fucked around with this message at 23:16 on Sep 24, 2008 |
# ? Sep 24, 2008 23:10 |
|
imBen posted:SQL Server 2005... code:
code:
|
# ? Sep 25, 2008 04:52 |
|
What's a good way (in MySQL) to impose an arbitrary order on the rows in a table, and still be able to use optimized index-range selects? What I have right now is a single table with data and an extra column containing a unique integer giving that row's position. However, if I do something like: code:
Is there some clever way, maybe involving multiple tables or sparsely-documented trickery, to get around this?
|
# ? Sep 25, 2008 09:07 |
|
Nomikos posted:What's a good way (in MySQL) to impose an arbitrary order on the rows in a table, and still be able to use optimized index-range selects? Where position > 0?
|
# ? Sep 25, 2008 13:41 |
|
In Oracle anyway, you could optimize that query by creating an index on (latitude,longitude,position). Even though you're not selecting based on position, because the index is sorted it will retrieve rows already ordered by position and thus save you the in-memory/on-disk sort you would otherwise suffer. Of course I have no idea if MySQL works this way...
|
# ? Sep 25, 2008 15:44 |
|
MoNsTeR posted:In Oracle anyway, you could optimize that query by creating an index on (latitude,longitude,position). Even though you're not selecting based on position, because the index is sorted it will retrieve rows already ordered by position and thus save you the in-memory/on-disk sort you would otherwise suffer. I just tried this and it didn't work. However, when I dropped all indexes on the table and removed the ORDER BY clause, performance improved by a factor of 10.
|
# ? Sep 25, 2008 21:01 |
|
What are the results of running 'explain' on your query? Maybe try making an index for 'position, longitude, latitude' and then do 'order by position, longitude, latitude'
|
# ? Sep 25, 2008 21:05 |
|
Begby posted:What are the results of running 'explain' on your query? Success! I made an index for (position, latitude, longitude) and now the original query no longer says "Using filesort" and runs quickly. If I remove the ORDER BY clause the results are still sorted correctly and it's even faster. So it looks like MoNsTeR's idea was basically correct, but I had to reverse the index order. Thanks for the help.
|
# ? Sep 25, 2008 21:19 |
|
Roseo posted:I'm trying to create a mySQL trigger that'll zero out some of the data for that row when the value of the status field for the row has changed. I've got the following code, which the server accepts and creates a trigger. However, it's not functioning properly, and I can't find any decent references for trigger syntax. Can anyone shed light on why this isn't working properly? code:
|
# ? Sep 26, 2008 09:29 |
|
MrHyde posted:I need to combine the results of two select statements into a single one. Both select statements select identical data. I tried using the UNION command but it results in a distinct type result set and I want to keep the repeated results. Is that possible?
|
# ? Sep 26, 2008 09:31 |
|
Victor posted:Do you request data from this 3mil record table via subquery or derived table/CTE expression? Here's what I run. code:
|
# ? Sep 26, 2008 17:34 |
|
imBen posted:Here's what I run. You are joining two tables on a key that is stored in a text field that you are converting to an integer. I can't see how that could ever be fast, I can't imagine it using any index at all since you are using convert. Secondly, is @Roles a string? If so can't you do AND USER_ROLE_ID IN ( @Roles )? That alone should save some processing time you would think. I am no SQL Server expert, but I would assume that parse function gets run for every record. I would assume the same thing for GETDATE(). It seems like you should pass in the current date as a value, or store it in the stored procedure in a variable. That way it doesn't run the function for every record (again, this may not be the case, I don't know how its getting optimized). Fourthly, you are doing an inner join on CONVERT( INT, ISNULL ( TEXT, 0 ) ) = CNT1.CNT_CONTENT_ID, then in your where clause you have AND TEXT = CNT1.CNT_CONTENT_ID. I may be missing something, but that seems redundant and I imagine its using an automatic cast of some sort. Perhaps you should try casting CNT1.CNT_CONTEND_ID into a string then comparing that to TEXT instead of converting TEXT to an int?? That is kind of a guess, but wortha shot.
|
# ? Sep 26, 2008 18:19 |
|
Pento McGreno posted:I just tested your trigger on a quick test database, and it worked for me. Here's the test I wrote. For reference, I also tested with status being a TEXT, same result. Can you a) see if this works for you, and b) point out any differences in your code? Thanks. The code wasn't the problem, the entry of it into the database is what was causing the problem. phpmyadmin must not like the use of delimiter commands, because when I used DELIMITER | in the SQL code to run it created the trigger, but it didn't function properly. Once I dropped the DELIMITER commands and just set it as | in the web UI, the trigger installed properly and runs fine. Figured it out when I ran your code and the server started spitting back syntax not understood errors. Roseo fucked around with this message at 19:15 on Sep 26, 2008 |
# ? Sep 26, 2008 19:12 |
|
Begby posted:Secondly, is @Roles a string? If so can't you do AND USER_ROLE_ID IN ( @Roles )? You can't do this in Oracle, or rather, you can only do it with some Clever Tricks. Does it work in other implementations? This would be very handy for a personal project.
|
# ? Sep 26, 2008 20:07 |
|
Begby posted:Secondly, is @Roles a string? If so can't you do AND USER_ROLE_ID IN ( @Roles )? That alone should save some processing time you would think. I am no SQL Server expert, but I would assume that parse function gets run for every record. SQL Server will let you do "column IN ( @StringVar )", but when you do it, it's equivalent to "column = @StringVar". If you have a comma delimited list, you have to write something to parse out each item individually, which is what he's doing there.
|
# ? Sep 26, 2008 20:24 |
|
|
# ? May 15, 2024 07:55 |
|
I'm making a simple forum using PHP. The following code will show topics in the order of when their first post was made. I need it to list the topics in the order of when their last post was made. How can I do this?code:
|
# ? Sep 27, 2008 03:38 |