|
Munkeymon posted:I'm doing this to an InnoDB table: I was interested in this, so I went and looked at a couple of the documentation pages. I agree that it doesn't sound like the behaviour you see is the behaviour described by the documentation. Out of curiosity: when you say "an insert with a blank lo_cookie", you mean lo_cookie is NULL, right? Have you tried making a similar table with lo_cookie declared NOT NULL, and seeing what happens in the same situation?
|
# ? Nov 10, 2009 23:37 |
|
|
# ? May 15, 2024 10:59 |
|
Hammerite posted:I was interested in this, so I went and looked at a couple of the documentation pages. I agree that it doesn't sound like the behaviour you see is the behaviour described by the documentation. It is NOT NULL, but it looks like I managed to omit that: code:
|
# ? Nov 11, 2009 16:30 |
|
Munkeymon posted:It is NOT NULL, but it looks like I managed to omit that: see this not a bug The man page isn't totally clear, but I think you need to do this: code:
|
# ? Nov 12, 2009 19:51 |
|
Jethro posted:You didn't say that you also had a primary key on the table. I see what they meant now by "you can work around this by using LAST_INSERT_ID(expr)." It wasn't clear to me at all from where the ID value is coming from or going to in their example. Thanks!
|
# ? Nov 12, 2009 20:33 |
|
Munkeymon posted:I see what they meant now by "you can work around this by using LAST_INSERT_ID(expr)." It wasn't clear to me at all from where the ID value is coming from or going to in their example.
|
# ? Nov 13, 2009 18:19 |
|
I'm really at a loss as to how something like this might be done without serious overhead, so I thought I might lob it out here, see if anyone has a solid solution. In SQL Server 2005, I'm working with a table that has a PK key sequential GUID called ID and an attribute for a parent item, ParentID. The parent item is the key GUID of the item that this item refers to, and is NULL for root items. Additionally, there is a datetime of when the record was added to the table, DateProcessed. These parent items cascade, such that, for example, we would have one root item with 2 children nodes 2 and 3, and then nodes 4 and 5 whose records reference them as the child of node 2 and 3, respectively. The table is very large, like 200,000+ records, and the size of each grouping is relatively small(most groups don't reach 5 records, though they could), giving me a large number of these groups. Given this, I want to return just the leaf node that was most recently inserted for each such grouping in the entire table, with a cap at 1000 of the most recent records returned. The best I've been able to come up with on this has been to rely on the fact that, by using newsequentialid(), the items more recently inserted have GUID values greater than previous items. Based on that, I was able to generate this: code:
If I move the TOP 1000 inside the CTE to the root expression, I get incredible performance gains, as you might expect. However, the next step, once I have this, is to add some additional where clauses to the outer query in order to provide the ability to search for certain items, and so the filtering needs to occur outside, or else I'm just left doing my search inside that TOP 1000 set instead of searching the whole set and placing a 1000 record limit on the results. Is there an easier way to do this that I'm just not seeing?
|
# ? Nov 13, 2009 22:33 |
|
I have a simple one.I'm not much of a SQL fellow, as you can probably guess from this question! I have a view with a list of companies.. Some companies have parent companies. dbo.company_view dbo.company_view.company_id = a numeric ID (unique) dbo.company_view.company_name= the common name of the company dbo.company_view.parent_company_id= the numeric ID of the parent (this keys off company_id) All I want to do, is take the parent_company_id, lookup the company_name, and spit that out to a new column named "parent_company_name_proper" or something like that. Any help would be greatly appeciated! Saltin fucked around with this message at 22:45 on Nov 13, 2009 |
# ? Nov 13, 2009 22:43 |
|
Saltin posted:I have a simple one.I'm not much of a SQL fellow, as you can probably guess from this question! If you just want the name of the parent company, then what I'd suggest is to do something like this: code:
|
# ? Nov 13, 2009 22:49 |
|
thefncrow posted:If you just want the name of the parent company, then what I'd suggest is to do something like this:
|
# ? Nov 13, 2009 22:57 |
|
thefncrow posted:I'm really at a loss as to how something like this might be done without serious overhead, so I thought I might lob it out here, see if anyone has a solid solution. I'm not up on performance, but couldn't you do something like this? SELECT TOP 1000 * FROM Items WHERE ParentId IS NOT NULL AND Id NOT IN (SELECT ParentId FROM Items) ORDER BY DateProcessed code:
Nurbs fucked around with this message at 05:38 on Nov 16, 2009 |
# ? Nov 16, 2009 05:12 |
|
thefncrow posted:I'm really at a loss as to how something like this might be done without serious overhead, so I thought I might lob it out here, see if anyone has a solid solution. code:
code:
If you really need only the most recent item from each group, then you could do this: code:
Finally, if you have the ability to change the schema, maybe add the GroupId so you don't have to generate it in a view all the time, though if it's flexible that may not be worth the trouble.
|
# ? Nov 16, 2009 16:59 |
|
I actually got this solved late on Friday, but I appreciate the help.Nurbs posted:I'm not up on performance, but couldn't you do something like this? That would pull back all leaf nodes on the tree. Imagine the circumstance Root -> Child -> (Grandchild1, Grandchild2). Grandchildren 1 and 2 both have ID values that don't appear as ParentID values, so they'd both be returned, instead of just returning the most recent of the two. Jethro posted:I don't know if this will be any faster, but why not have the ItemGroup in your CTE just be the ID of the top parent item? That way you don't have to do the analytic function. This was actually part of my eventual solution. I went with the group item as the ID value of the root node, and, sloppy as it is, I flattened out the CTE to rid myself of it entirely. That meant losing recusion and going to only a fixed number of levels, but based on what the Items table does, it turned out it would already begin experiencing weirdness if we ever go beyond 3 levels of tree depth. So, I compromised on that and went with 3 levels of depth instead of building it recursively, and I ended up with something like code:
|
# ? Nov 16, 2009 20:50 |
|
I've some problems to do with table and index design. As backstory: I have a web database application, the database side being MySQL with MyISAM tables. When I was designing the database schema I was inexperienced at working with databases, and so I made the odd suboptimal choice here and there. I now want to correct the design, but since the application is in use, this requires a certain amount of planning. I also intend on changing to InnoDB tables. This is to be done after the schema redesign has gone into effect, but some of the redesign is with the switch to InnoDB in mind. I've read some articles and manual pages on how to design things for InnoDB but can't be 100% confident everything is ship shape in my design. There is one specific table for which I'm not sure what the correct design choices are, though. This table is named PlayerGameRcd and it records player participation in a game. So I have a table User with autoincrementing primary key UserID, and a table Game with autoincrementing primary key GameID, and the primary key for PlayerGameRcd is (Game, User) where Game is a column referencing GameID and User is a column referencing UserID. This table currently has about 18,000 rows. The source of difficulty is that I have a lot of things in mind to use this table for (that it's already being used for, in fact). I want to use it for: - Querying to get a list of games that a player is playing in, is waiting to play in, or has played in (if a user "has played in" a game, then either the game is finished, or the user has quit out of it or been removed). - Querying to find out how many games a user played, and how many he won. (When I do this, I want to grab some details about the games themselves, so this involves a join to Game.) - Querying to find out what the best scores are in certain categories of game. In order to know whether I should count a row in a particular category, I need to know whether the game is marked as "doesn't count"; I may also need to know some combination of whether the game was won and whether it was lost. (These are multiplayer games; I use the word "lost" to mean that the player came in last place, as opposed to merely being a runner-up.) - Querying to find a user's personal best scores, with the same restrictions as previously discussed. This set of several different uses means that I have several different indexes in mind for the table, but I am worried I have planned too many indexes. Here is the planned schema for the table post-redesign and post-switch to InnoDB: code:
I'm also kind of weak on just how MySQL makes use of indexes on tables when they can be used both to slim down the result set and to perform a join; I'm not sure where to learn about that. Nor am I sure whether it uses them when they can be of help in implementing an ORDER BY clause. (I am aware that only one index is used for each table occurring in a query.) As an example, here is a query that I might use to rank losing scores in three-player games on a particular game board: code:
|
# ? Nov 18, 2009 01:37 |
|
18,000 rows is really nothing that shouldn't even matter as long as you index it by the most common fields you will use in your where.
Sprawl fucked around with this message at 02:07 on Nov 18, 2009 |
# ? Nov 18, 2009 02:04 |
|
quote!= edit
|
# ? Nov 18, 2009 02:06 |
|
If the cardinality of the indexed column is low a b-tree index is actually going to hurt performance anyway, and what Sprawl said about 18k rows being small is absolutely true. If you're selecting based on a column with high cardinality (like a PK) then the opposite is true of course.
|
# ? Nov 18, 2009 02:52 |
|
Markoff Chaney posted:If the cardinality of the indexed column is low a b-tree index is actually going to hurt performance anyway, and what Sprawl said about 18k rows being small is absolutely true. If you're selecting based on a column with high cardinality (like a PK) then the opposite is true of course. Does this remain true if I have an index on multiple columns, a mixture of high-cardinality and low-cardinality? Based on your and Sprawl's responses, it seems as though the columns and indices GameWon and GameLost (and the column GameCompleted) are not worthwhile. The other indices are all ones that are either necessary (the PRIMARY KEY and the other UNIQUE KEY) or will be frequently-used; GameList_ind will be used to find all games a given user is involved in. (Also, if GameList_ind didn't exist then InnoDB would make a new index for the foreign key constraint on User, anyway.) But I'm unsure whether to reinstate an earlier idea, which was to have an index on (GameCounts, GameResult, User) [which in InnoDB is implicitly a key on (GameCounts, GameResult, User, Game)]. GameCounts and GameResult have middling to low cardinality (GameCounts is treated as a Boolean value), but the proposed index could nevertheless be used for some queries; particularly when the value of User is of interest too (looking for a user's personal data). Actually, I think I just realised what I need to do: have an index on (GameCounts, GameResult, Score, User). This is assuming that indices on columns that will appear in an ORDER BY clause are useful; I should do some experiments to check this.
|
# ? Nov 18, 2009 12:00 |
|
Hammerite posted:Does this remain true if I have an index on multiple columns, a mixture of high-cardinality and low-cardinality? They would if your dataset was long enough maybe but i doubt you would see anything but a few MS here and there i wouldn't expect any results unless you had lots of concats to take more then 100ms.
|
# ? Nov 18, 2009 15:31 |
|
Hammerite posted:... So the indexes would be taking up about twice as much space as the table itself, which seems like some kind of warning sign that something is wrong with this plan. ... This is pretty common. Here is the size breakdown for one of my company's core tables (which mostly just associates two other things and tracks a status): table: 10372MB PK index: 2780MB create date index: 3452MB another date index: 3656MB status date index: 3968MB index on one associated entity id, and status: 4448MB index on both associated entity ids: 3436MB (index total): 21740MB And of course at these sizes, we really do need these indexes for performance (the tables is also partitioned but that's another story).
|
# ? Nov 18, 2009 16:57 |
|
Thanks guys. More-or-less unrelated question: If I put the function UTC_TIMESTAMP into a WHERE clause, is it recalculated for each row (after all, it might have changed by milliseconds in the meantime!) or calculated only once for the query (as seems sensible)? So if I issue a query in which part of the WHERE clause reads, for example, code:
EDIT: I found where in the documentation it says this not a minute after typing this. (The answer is it only evaluates it once.) Hammerite fucked around with this message at 00:52 on Nov 19, 2009 |
# ? Nov 19, 2009 00:50 |
What is the correct way to do what I am trying to do? This obviously won't work, since I can only have 1 field in the subquery: code:
code:
|
|
# ? Nov 19, 2009 01:19 |
|
fletcher posted:What is the correct way to do what I am trying to do? Something like this (untested)? code:
|
# ? Nov 19, 2009 02:08 |
camels posted:Something like this (untested)? Worked great! Thanks!
|
|
# ? Nov 19, 2009 19:29 |
I have a VARCHAR(1300) column that I want to sort my results by. There wasn't an index on it so it was pretty slow, but when I try to add an index to it I get: "Specified key was too long; max key length is 1000 bytes" Is there another way to get the query to run faster?
|
|
# ? Nov 19, 2009 23:12 |
|
fletcher posted:I have a VARCHAR(1300) column that I want to sort my results by. There wasn't an index on it so it was pretty slow, but when I try to add an index to it I get: "Specified key was too long; max key length is 1000 bytes" use a fulltext index? or convert it to a text field and put a fulltext index on it?
|
# ? Nov 19, 2009 23:20 |
Sprawl posted:use a fulltext index? or convert it to a text field and put a fulltext index on it? I tried using a fulltext index, but it was still slow, and EXPLAIN said it wasn't using an index. I also tried specifying with USE INDEX() and it was still slow. Same results whether it's a VARCHAR or TEXT field.
|
|
# ? Nov 19, 2009 23:32 |
|
You could maybe create a column of much shorter length (anywhere < 1000, I'm guessing?) that will act as the sort key in lieu of the actual content column. Then cluster index by this and sort by it.code:
|
# ? Nov 19, 2009 23:47 |
|
nevermind
nbv4 fucked around with this message at 02:43 on Nov 20, 2009 |
# ? Nov 20, 2009 02:35 |
|
I'm new to SQL commands and I'm currently writing a VB program utilizing them... I need to display rows of Product Name, Unit Price, Quantity, and Total (of unit price * quantity) and then at the very bottom a grand total which would be the SUM of the Totals which I gave the alias Product_Total here's my code: code:
Any help on grasping how to accomplish this would be wondrous. KhanTusion fucked around with this message at 10:02 on Nov 20, 2009 |
# ? Nov 20, 2009 09:58 |
|
If you were using MySQL, I know for a fact that that query won't work as-is because a subquery must have an alias. If you take your query as it is and enter " AS SubqueryA" after the final bracket, then it would work in MySQL (with some trivial differences in notation). Could that be the problem here? MySQL would explicitly tell you that the subquery needs an alias, but maybe other DBMSs have the same requirement but no explicit error message about it. Edit: Also, you seem to be SELECTing product_name and then not using it. In fact, you don't actually need to use a subquery at all; you can use code:
Hammerite fucked around with this message at 13:56 on Nov 20, 2009 |
# ? Nov 20, 2009 13:53 |
|
KhanTusion posted:I'm new to SQL commands and I'm currently writing a VB program utilizing them... code:
code:
|
# ? Nov 20, 2009 17:34 |
|
thanks for the help, from what you've said I decided to just create a subroutine in VB to run a second query to aggregate the sum. Even with those code examples you gave me I couldn't get it to work all in one query so I scrapped it. Working now though, thanks.
|
# ? Nov 20, 2009 22:04 |
|
KhanTusion posted:thanks for the help, from what you've said I decided to just create a subroutine in VB to run a second query to aggregate the sum. Even with those code examples you gave me I couldn't get it to work all in one query so I scrapped it. Working now though, thanks. Really that's odd i have a working one here i created to make sure it was gonna work before i pasted that hth. code:
code:
|
# ? Nov 20, 2009 22:15 |
|
What about GROUP BY ... WITH ROLLUP? That gives an extra row at the end with the SUM of number fields.
|
# ? Nov 21, 2009 02:08 |
|
I hope this is the right place to ask about this. I figure some folks here know a good bit about SQLServer diagnostics. I'm looking for some tips on how best to diagnose a problem... We have a ASP.NET Testing website (built using NHibernate) running against SQL Server Express on our QA box. It's got about 40 users right now. Every once in a while (twice over the last 2 months), something locks up one of the tables in the db-- the "SiteUSERS" table. Any access to this table times out, even the simplest of queries directly from the Management Studio console. I can go to the Activity monitor in management studio and see that there are two processes accessing the DB, and one is suspended, waiting on the other. If I kill that process, everything goes back to normal. But how do I figure out the root cause? Is there some way to see why that process is locked? The table is used in a lot of places on the site. I don't have direct control over the SQL statements used by the site, NHibernate is generating those. I could enable full logging on NHibernate, but that's a LOT of information to sift through. Next time this happens, what information should I gather to help find the root cause?
|
# ? Nov 21, 2009 22:29 |
|
Dromio posted:We have a ASP.NET Testing website (built using NHibernate) running against SQL Server Express on our QA box. It's got about 40 users right now. 1. Identify the SQL statement that is blocking. This approach is the same (I think) whether you are using SQL Server 2005 Express or SQL Server 2008 Express. Use SQL Trace to capture the offending SQL statement. I'm pretty sure SQL Server Express does not support the GUI tool (Profiler), but it does support the system stored procedures for running a trace. Here's an example: http://www.mssqltips.com/tip.asp?tip=1035 Limiting the trace to long running queries where the SQL Text is like the table name will probably narrow it down enough. 2. With this info, perhaps you could start a support incident with NHibernate? I'm not familiar with them. I take it that you can't modify the SQL Statements or server settings. Perhaps there are missing indexes that would solve the problem, assuming you can add indexes. http://www.sql-server-performance.com/tips/blocking_p1.aspx http://www.sql-server-performance.com/tips/reducing_locks_p1.aspx edit If you are at SSMS when the blocking is occurring, try this: 1. Get the object id of the table in question: code:
code:
code:
Then to get the offending SQL statement, use code:
Bad Titty Puker fucked around with this message at 03:51 on Nov 22, 2009 |
# ? Nov 22, 2009 02:59 |
|
Thank you camels, this looks like it will help a lot! If only I could force the issue to happen again sooner
|
# ? Nov 22, 2009 16:32 |
|
I'm trying to pull data out of a dozen different tables or so and put them into a format that is actually part of the industry standard. The standard in question is called Multispeak and, at the end of the day for this conversion, what I need is an XML file that conforms to the XML schema (.xsd) that I have for Multispeak. Is there an easy way of pulling data from MS SQL into an XML file by mapping particular table fields to an XSD file? So far, all I have come up with is this near-decade-old article on MSDN; the sample code doesn't appear to work when I try to modify the .xsd they provide to reflect the tables I am pulling from. My other option is using a standard XML parsing library (.NET or Python's lxml) to write it out myself, but it feels like there is an easier way out there I am missing just using SQL Server and thought I would ask. Thanks.
|
# ? Nov 23, 2009 18:17 |
|
Gallipoli posted:I'm trying to pull data out of a dozen different tables or so and put them into a format that is actually part of the industry standard.
|
# ? Nov 23, 2009 18:56 |
|
|
# ? May 15, 2024 10:59 |
|
I apologise if this isn't the right place to ask, I was wondering if anyone knows of any good resources (books or online) for learning SQL? There's so many books and sites available, I'm not sure which would be best for a beginner. I have never done programming before, I'm just learning as I hear it's quite useful in business analysis. Should I focus on mastering SELECT, or is it useful knowing how to construct databases?
|
# ? Nov 23, 2009 20:07 |