database-queries

Is it possible to break a Composite key... help

Is it possible to break the composite key of a table and use one of them as a primary key for other table? if YES plz tel how can i do it.. ...

PostgreSQL: Full Text Search - How to search partial words ?

Hello, Following a question posted here about how I can increase the speed on one of my SQL Search methods, I was advised to update my table to make use of Full Text Search. This is what I have now done, using Gist indexes to make searching faster. On some of the "plain" queries I have noticed a marked increase which I am very happy abo...

When are ENUMS joined in in PostgreSQL

If I run a select from a column in PostgreSQL that is of type enum, are the string values of the enum joined in before or after the final result set has been created? An explanation of my motive can be found at http://archives.postgresql.org/pgsql-performance/2008-07/msg00226.php ...

Rails : fighting long http response times with ajax. Is it a good idea? Please, help with implementation details.

Hi, everybody! I've googled some tutorials, browsed some SO answers, and was unable to find a recipe for my problem. I'm writing a web site which is supposed to display almost realtime stock chart. Data is stored in constantly updating MySQL database, I wrote a find_by_sql query code which fetches all the data I need to get my chart dr...

Cache the result of a MySQLdb database query in memory

Our application fetches the correct database server from a pool of database servers. So each query is really 2 queries, and they look like this: Fetch the correct DB server Execute the query We do this so we can take DB servers online and offline as necessary, as well as for load-balancing. But the first query seems like it could b...

PHP, MySQL, and temporary tables

Php novice. 1.Is there anything wrong with this PHP & MySQL code? include_once "db_login.php" ; $sql = "DROP TEMPORARY TABLE IF EXISTS temp_sap_id_select" ; mysql_query ( $sql ) or ( "Error " . mysql_error () ) ; $sql = " CREATE TEMPORARY TABLE temp_sap_id_select ( `current_page` INT NOT NULL, ...

How do I use Linq-to-sql to iterate db records?

I asked on SO a few days ago what was the simplest quickest way to build a wrapper around a recently completed database. I took the advice and used sqlmetal to build linq classes around my database design. Now I am having two problems. One, I don't know LINQ. And, two, I have been shocked to realize how hard it is to learn. I have...

PostgreSQL subselect problem

When I try to do the select statement, I always get the following error: ERROR: more than one row returned by a subquery used as an expression Only if there's only one result, it works. Why and how to fix? SELECT name from person p where id = ( select prs from leader where age(leader.lastcourse) > '1 year'); ...

Optimize a MySQL count each duplicate Query

I have the following query That gets the city name, city id, the region name, and a count of duplicate names for that record: SELECT Country_CA.City AS currentCity, Country_CA.CityID, globe_region.region_name, ( SELECT count(Country_CA.City) FROM Country_CA WHERE City LIKE currentCity ) as counter FROM Country_CA LEFT JOIN glo...

How to optimize an SQL query with many thousands of WHERE clauses

I have a series of queries against a very mega large database, and I have hundreds-of-thousands of ORs in WHERE clauses. What is the best and easiest way to optimize such SQL queries? I found some articles about creating temporary tables and using joins, but I am unsure. I'm new to serious SQL, and have been cutting and pasting result...

What are some useful SQL statements / usage patterns that should be known by all developers who may touch the Back end side of the project?

What are some useful SQL statements that should be known by all developers who may touch the Back end side of the project? (Update: just like in algorithm, we know there are sorting problems, shuffling problems, and we know some solutions to them. This question is aiming at the same thing). For example, one I can think of are: Get...

Convert charset in mysql query

Hi, I have a question about converting charset from inside mysql query. I have a 2 databases. One for the website (joomla), the other for forum (IPB). I am doing query from inside joomla, which by default have "SET NAMES UTF8". I want to query a table inside the forum databases. A table called "ibf_topics". This table has latin1 encodi...

Need help with SQL query on SQL Server 2005

We're seeing strange behavior when running two versions of a query on SQL Server 2005: version A: SELECT otherattributes.* FROM listcontacts JOIN otherattributes ON listcontacts.contactId = otherattributes.contactId WHERE listcontacts.listid = 1234 ORDER BY name ASC version B: DECLARE @Id AS INT; SET @Id = 1234; SELECT otherattribut...

Thoughts on a Shoutbox anyone?

I'm wanting to create a shoutbox, though I'm wondering if there is another way to go about this rather than using setInterval to query the database for new shouts every number of seconds. Honestly, I don't like having to go about it this way. Seems a bit redundant and repetitive and just plain old wrong. Not to mention the blinking of...

how to do gedcom import with minimal database roundtrip. what is best practice for this kind of development

In My current application, I need to import users from gedcom file. these users may exist in my registered users or i need to create one registered user for the same. now gedcom file contain s many information e.g. PersonalDetails,Addresses, Education Details, ProfessionalDetails this is one sample of xml file we are storing to store us...

Finding first alphabetic character in a DB2 database field

I'm doing a bit of work which requires me to truncate DB2 character-based fields. Essentially, I need to discard all text which is found at or after the first alphabetic character. e.g. 102048994BLAHBLAHBLAH becomes:- 102048994 In SQL Server, this would be a doddle - PATINDEX would swoop in and save the day. Much celebration would...

Can I ask Postgresql to ignore errors within a transaction

I use Postgresql with the PostGIS extensions for ad-hoc spatial analysis. I generally construct and issue SQL queries by hand from within psql. I always wrap an analysis session within a transaction, so if I issue a destructive query I can roll it back. However, when I issue a query that contains an error, it cancels the transaction. ...

postgresql syntax while exists loop

I'm working at function from Joe Celkos book - Trees and Hierarchies in SQL for Smarties I'm trying to delete a subtree from an adjacency list but part my function is not working yet. WHILE EXISTS –– mark leaf nodes (SELECT * FROM OrgChart WHERE boss_emp_nbr = −99999 AND emp_nbr > −99999) LOOP –– get list of next...

How to insert an n:m-relationship with technical primary keys generated by a sequence?

Let's say I have two tables with several fields and in every table there is a primary key which is a technical id generated by a database sequence: table1 table2 ------------- ------------- field11 <pk> field21 <pk> field12 field22 field11 and field21 are generated by sequences. Also there is a n:...

Implementing "select distinct ... from ..." over a list of Python dictionaries

Hi folks, Here is my problem: I have a list of Python dictionaries of identical form, that are meant to represent the rows of a table in a database, something like this: [ {'ID': 1, 'NAME': 'Joe', 'CLASS': '8th', ... }, {'ID': 1, 'NAME': 'Joe', 'CLASS': '11th', ... }, ...] I have already written a function to ge...