|
Subotai posted:I am using sql server 2008 and storing the datetime in one of my tables using the GETUTCDATE() function. Is there anyway to pull the time out of the database so that is not a literal string value but instead an integer value, like the number of seconds from the epoch, etc? After looking at all the date time functions on MSDN, I don't see a way to do this, but it seems like it would be something a lot of people would use. datediff(s,'01-Jan-1970',getutcdate()) E:F,B
|
# ? Mar 18, 2009 21:44 |
|
|
# ? Jun 5, 2024 17:49 |
|
goddamnit nm!
eHacked fucked around with this message at 17:58 on Mar 19, 2009 |
# ? Mar 19, 2009 17:51 |
|
This is a really easy one, but nigh unsearchable on Google: What does AS do in an Oracle command? For example:code:
|
# ? Mar 19, 2009 21:03 |
|
Morpheus posted:This is a really easy one, but nigh unsearchable on Google: What does AS do in an Oracle command? For example: Renames columns. You might be interested in Oracle's SQL reference document. Say one thing for Oracle - say that they produce very thorough and verbose documentation. http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2065646
|
# ? Mar 19, 2009 21:11 |
|
Morpheus posted:This is a really easy one, but nigh unsearchable on Google: What does AS do in an Oracle command? For example: Specifically, in a totally non-technical way, it assigns a column name to the value. The value is the value of the expression "columnName" (or it could be something more complex like "columnName + 1". Most people ignore this since for simple expressions, the column gets autonamed with the column used in the expression. But if you use two column names to calculate a new value, the column won't actually be named anything, which is okay for certain operations. For others, you absolutely do need a name for the colum.
|
# ? Mar 20, 2009 03:43 |
How can you speed up a query such as the following? (MySQL)code:
|
|
# ? Mar 20, 2009 08:42 |
|
Add a clustered index articleId And an index timestamp, value And then make sure that this is actually works. (Sample runtime before, and then after.)
|
# ? Mar 20, 2009 21:06 |
|
The above is wrong -- I must have skimmed the CREATE INDEX documentation and miscomprehended the "unique". Victor fucked around with this message at 02:57 on Mar 21, 2009 |
# ? Mar 21, 2009 00:00 |
|
Victor posted:clustered indexes must be unique False.
|
# ? Mar 21, 2009 02:06 |
|
I'd like to humbly interject that it's hard to give good advice without a minimum of: *DDL, including keys and indexes *A query execution plan. Sample data would also be nice to have.
|
# ? Mar 21, 2009 03:10 |
|
I have a MySQL administrative question for Linux. I keep getting bad rows in a table I'm using for a toy app I'm running, but I seem to notice new problems that require a repair after booting up. I assume then that I'm improperly shutting down my system with mysql install. Generally I just run 'sudo halt' and wash my hands of it. Should I be shutting down in another manner to elegantly terminate SQL and make sure it's happy before halting the system?
|
# ? Mar 22, 2009 06:56 |
|
So, let's say I have a table of customers and each customer needs to get an order number that is only unique to that customer (customerID=1 would have customerOrderID=1,2,3..etc while customerID=2 would have customerOrderID=1,2,3,... in the same table). I'd imagine determining the next customerOrdeID would just using MAX() (or COUNT()) on the column and incrementing it by 1, but what would be easiest way to do this to prevent concurrency problems? I was thinking making this a stored procedure and using a transaction, but if I did this, what isolation level would be sufficient (SQL Server 2005)?
|
# ? Mar 22, 2009 18:50 |
|
Rocko Bonaparte posted:I have a MySQL administrative question for Linux. I keep getting bad rows in a table I'm using for a toy app I'm running, but I seem to notice new problems that require a repair after booting up. I assume then that I'm improperly shutting down my system with mysql install. Generally I just run 'sudo halt' and wash my hands of it. Should I be shutting down in another manner to elegantly terminate SQL and make sure it's happy before halting the system? This can vary by distribution, but most SystemV style distributions execute a series of shutdown scripts at the runlevel 0 and 6 for (halt and reboot respectively), they are located in /etc/rc.d/, usually. The most common problem is the lack of an entry in either 0, 6, or both.
|
# ? Mar 22, 2009 19:36 |
|
This type of problem comes up enough for me, and I am getting sick of all the manual work it takes to get the format I need. I am looking at a list of matches made by our system, and i specifically want to see where there are cases where multiple ID's match to a single secondary unique ID: code:
code:
i have tried various forms of joins, etc, but it just ends up with me doing a LOT of temp tables with the ranks seperated, and then just a few joins, and then one big union, and then distinct across the whole thing. There has got to be a better way.. but my goggle and oracle foo is failing me.. Ideas?
|
# ? Mar 25, 2009 18:36 |
|
Roundboy posted:This type of problem comes up enough for me, and I am getting sick of all the manual work it takes to get the format I need. If you have Oracle 11 you can use the pivot operator. If you have pre-11 you can synthesize a result using a pattern following code:
|
# ? Mar 25, 2009 18:49 |
|
var1ety posted:If you have Oracle 11 you can use the pivot operator. If you have pre-11 you can synthesize a result using a pattern following This worked quite well (but you are missing 'end' in those case statements. i determined that I only need a max of '4' rownums, but the majority is just 1&2. Thanks, I don't know why this was so complex to figure out.
|
# ? Mar 25, 2009 19:47 |
|
Looking to reduce my app to a single query, but I have one difficulty. Say I have a table "users":code:
code:
It's an Oracle 9 database, if that matters.
|
# ? Mar 25, 2009 22:48 |
|
FeloniousDrunk posted:Looking to reduce my app to a single query, but I have one difficulty. Say I have a table "users": select a.user,a.id,b.status from USER a inner join STATUS b on a.ID = b.ID and b.status = '1' But, I can see that there are TWO users with a status of '1', Fred and Hank .. what other limiters do you have ? The above will return all users with a status of 1
|
# ? Mar 26, 2009 00:56 |
|
Roundboy posted:select a.user,a.id,b.status Yeah, I think I need a subquery somehow to kick out the unwanted users. I'm messing around with things like: code:
|
# ? Mar 26, 2009 01:13 |
|
FeloniousDrunk posted:Looking to reduce my app to a single query, but I have one difficulty. Say I have a table "users": Something like this (untested)? code:
|
# ? Mar 26, 2009 01:50 |
|
camels posted:Something like this (untested)? This looks reasonable, though I think maybe it will be more efficient if I turn it inside-out. Though maybe not: are subqueries executed before the outside query, or is there magic involved? I might as well share the real situation. Here's the main thing: code:
code:
|
# ? Mar 26, 2009 06:02 |
|
Well, as a general rule a set-based query is going to outperform a row-at-a-time solution. If you write the row-based query as a PL/SQL script - I don't know if you can see the explain plan for the whole thing, but if you can, you can compare it to the plan for the single-query approach. Ideally, you can write it both ways and run a trace, and compare reads and CPU. The explain plan for the single-query approach will answer your questions about how the database engine puts it together. If you don't mind posting DDL and especially indexes, along w/ the test results, it sounds interesting. I'm guessing that you will get a scan on "ITEM" (not to be annoying, but that is a p. bad name for a table) because of this: code:
|
# ? Mar 26, 2009 09:26 |
|
camels posted:Helpful things OK, you definitely have it over me in geek points. I don't know the first thing about PL/SQL or what a DDL is, really I'm just a web developer dealing with a monstrous integrated library system database ("ITEM" was not my decision -- there's more cryptic stuff than that in the 403 friggin' tables). I do have this "TOAD" thing that certainly looks like it should give me what you ask, though, if I only knew where to look. Heck, it might even have a query optimizer. Anyhow, this is what I came up with this morning: code:
Results (times are cumulative): code:
20 days: 113512 raw records in 159.596s 5 days: 113509 raw records in 171.720s I'm assuming the variation in number of records returned is because it's a live database. If I recall correctly the previous looping approach took about 4 minutes, so this is an improvement. I still feel it could be better though...
|
# ? Mar 26, 2009 18:08 |
|
DDL is the CREATE TABLE statements for the tables, including any indexes and foreign keys. PL/SQL is just Oracle's proprietary brand of SQL; I speculated that you could write your looping query in it and get trace results that way. In Oracle the command EXPLAIN PLAN FOR (sql statement) will show the query plan that the optimizer came up with. btw TOAD is awesome, I actually used to work for the company that makes it (Quest Software) but on a different product that only did SQL tuning. I haven't played with it for over ten years, but I think it has some handy ways to do some of the above things. Anyway, something tells me that the DDL would fill several reams of paper though. Here's something -- and I am not sure that it is equivalent to your query -- but it's worth a shot. Can you combine the outer query into the subquery, something like this: code:
|
# ? Mar 26, 2009 18:55 |
|
FeloniousDrunk posted:This looks reasonable, though I think maybe it will be more efficient if I turn it inside-out. Though maybe not: are subqueries executed before the outside query, or is there magic involved? The best approach to take is going to depend on your item distribution and the indexes you have available (and whether or not you can create new ones). I would try starting by retrieving all the items which match your "has status = 1 and no other status" qualifier, and then pare this list down in successive filter operations. code:
var1ety fucked around with this message at 19:13 on Mar 26, 2009 |
# ? Mar 26, 2009 19:02 |
|
camels posted:DDL is the CREATE TABLE statements for the tables, including any indexes and foreign keys. PL/SQL is just Oracle's proprietary brand of SQL; I speculated that you could write your looping query in it and get trace results that way. In Oracle the command EXPLAIN PLAN FOR (sql statement) will show the query plan that the optimizer came up with. btw TOAD is awesome, I actually used to work for the company that makes it (Quest Software) but on a different product that only did SQL tuning. I haven't played with it for over ten years, but I think it has some handy ways to do some of the above things. Ooh, 113465 raw records in 178.945s for 20-day chunks. Pity, because yours looks like it should be more efficient. Maybe it's just server load; I'll keep yours for now. It only runs twice a day, so three minutes should be OK. I poked the TOAD and it doesn't seem to be able to improve on yours ("best alternative execution plan" is blank).
|
# ? Mar 26, 2009 19:54 |
|
This is a horribly vague question and I apologize for that, but I'm not a database programmer and our DB guys are out until Tuesday so I'm uncertain what to google for. (I'm a C++ programmer who doesn't know a whole hell of a lot about databases.) Is it as insane as I think it is to want to store SQL queries as a string in a database table? I'm in a situation where I may need to look up an appropriate query for certain situations, rather than hardcoding it into my program. So just a table with two columns, an index and a varchar or whatever containing the query I want, if that makes sense.
|
# ? Mar 26, 2009 23:13 |
|
Ledneh posted:This is a horribly vague question and I apologize for that, but I'm not a database programmer and our DB guys are out until Tuesday so I'm uncertain what to google for. (I'm a C++ programmer who doesn't know a whole hell of a lot about databases.) Generally you want to just create stored procedures to do the things you want. Why would you need to "look up an appropriate query"? If they were well-named stored procedures, you'd just choose the right one.
|
# ? Mar 26, 2009 23:17 |
|
No Safe Word posted:Generally you want to just create stored procedures to do the things you want. Why would you need to "look up an appropriate query"? If they were well-named stored procedures, you'd just choose the right one. It's the choosing the right one part that's... well, normally I'd just hardcode in the query to use for a given situation in my program. But the Powers That Be have raised the question "Why don't we use a DB lookup to determine the correct stored procedure instead of hardcoding it, so we can extend the program without having to change or recompile it?", and I'm trying to provide an answer either way. And they have a point, because there are a LOT of "given situations" that would take me an aeon to describe in text. But anyway. My first thought was "ok, just make a table of IDs to SQL queries" (or I guess stored procedures). My second thought was "wait, that's loving stupid." My third thought was "I don't know dick about databases " and so I came here. That make any sense?
|
# ? Mar 26, 2009 23:26 |
|
Ledneh posted:"Why don't we use a DB lookup to determine the correct stored procedure instead of hardcoding it, so we can extend the program without having to change or recompile it?" Because that's retarded. You cannot "look up in a database" what query you want to run for an arbitrary number of situations. If you find that your product matches a certain type of database (and it probably does) like OLTP/ORM access, then you can probably offload your poo poo on to a framework. Sounds like you (collectively) are in over your heads and need someone to start specializing in DB ish.
|
# ? Mar 27, 2009 00:10 |
|
Ledneh posted:"Why don't we use a DB lookup to determine the correct stored procedure instead of hardcoding it, so we can extend the program without having to change or recompile it?" My understanding is that you want to be able to dynamically change the SQL used in a DB lookup. If you have a fixed set of values then you can use a lookup scheme as you're thinking of. Of course, each time you want a new query you're going to have to add it to the database. In my opinion this solution is relatively simple but there isn't much to gain except for avoiding compiles. If however you want to dynamically build some SQL for different situations then that isn't so hard. You need to figure out how you're going to represent your SQL in a format that doesn't require compilation and that can be read in dynamically (say as an external XML file). You could either simply type in the SQL into an XML file, or you can use some other type of representation that can be converted to SQL. Then, you can read in the SQL and execute it (lookup prepared statements). This has more to gain in my opinion because you can create completely new queries without having to go into the DB and make stored procedures.
|
# ? Mar 27, 2009 12:02 |
|
I've got a solution I'm pondering in MySQL. I've got a page in PHP that's displaying a list of rows from a MySQL database, which have a one-to-many relationship to other tables in the database (it's actually a list of college courses, which can have multiple crosslistings, multiple professors, etc. associated with them). What I'm trying to figure out is what's the most efficient way to pull those relations into the view (i.e., print out that course XXX123 is taught by professors A and B and is crosslisted as YYY456, for each course XXX123 in a list of many). I can do it pretty easily with a LEFT JOIN and GROUP_CONCAT, but, say, if I want to add links to a page based on their primary keys, it gets ugly, but I think I can still do this (hackishly) with GROUP_CONCAT. The only other possibility I can think of is running another query for each individual item, but I'd rather hack about with GROUP_CONCAT since the overhead of calling that many queries definitely isn't worth it. Then again though, I can't see any other way than the two I've outlined. Any thoughts?
|
# ? Mar 27, 2009 18:19 |
|
I don't understand. Do two LEFT JOINs?
|
# ? Mar 27, 2009 19:03 |
|
Triple Tech posted:I don't understand. Do two LEFT JOINs?
|
# ? Mar 27, 2009 19:15 |
|
I have a Contacts table which has fields for ID, [ForeignKey], Name, Title, Phone, EMail & a boolean IsPrimary, which (duh) signals that this is the contact to notify first if need be. The FK is One-To-Many with the Contacts table being the many, so is there a way to set up an index to only allow 1 IsPrimary to be set to true for a foreign key? edit: MSSQL 2005 Potassium Problems fucked around with this message at 19:58 on Mar 27, 2009 |
# ? Mar 27, 2009 19:54 |
|
I don't understand your example. Foreign key to what? You only mention one object, a Contact. An index is something you add to a table to make reading from it faster. An exception case is a uniqueness constraint, which is implemented by a unique index. Conflated in SQL Server, but still two different concepts. I can't think of a query off the top of my head, but you can definitely enforce a "constraint" with a trigger, with which there are less limits on query complexity.
|
# ? Mar 27, 2009 20:34 |
|
The primary table is Clients, with the unique ID for the Clients table being the FK in the Contacts table. I tried a unique constraint, but it ended up only allowing 2 Contacts per Client; one with IsPrimary set to true, and one with IsPrimary set to false. I'm thinking it might be easier to enforce this in my data layer, but I'll look into a constraint with a trigger.
|
# ? Mar 27, 2009 21:07 |
|
My web application allows people to play a board game against each other over the internet in a play-by-email type of format. There are two tables we are concerned with, Game and User. There are five different colours or "seats" for players in the game: Red, Yellow, Green, Purple, and White. A row in the Game table stores information on who is playing in each game as follows: there are five columns PlayerR, PlayerY, PlayerG, PlayerP and PlayerW which store the user ID of the player in that seat, or are null if that seat is unfilled. So for example we might havecode:
code:
My question is, is this method of using five LEFT JOINs makedly inefficient and is there a much better way of doing it? Using MySQL.
|
# ? Mar 28, 2009 12:54 |
|
Stack the user ids vertically in the user id/game id xref table.code:
|
# ? Mar 28, 2009 13:28 |
|
|
# ? Jun 5, 2024 17:49 |
|
I'm toying around with writing something to visually represent a lot of data, and I figure a database might be a good way to do the actual storage of the data. Having never working with databases before, though, I have a few questions: 1. I'm going to need to use Foreign Keys to represent the links between different sets of data (students with schools, for example). However, there are at least a few relations in my data that could need multiple FKs in the same field (i.e. having 2 co-valedictorians). Is there a way to have one column have an arbitrary number of FKs in it? If not, do you have any suggestions for dealing with stuff like this? 2. Is there a way to make a "Last Updated" field for every row that updates anytime something in that row is changed? Rows get updated individually and periodically; it'd be nice if I could find which entries are potentially the most outdated. 3. Do you guys have any preferred gui editors/admin things? I was poking around with OpenOffice Database, but I couldn't figure out how to do FKs, which is probably due to my lack of sleep.
|
# ? Mar 29, 2009 09:09 |