Jump to content

help with query


bagnallc

Recommended Posts

Hi,

 

I have a table which holds records of occurences, which is very simple

 

records

 

recordid varchar(20) not null primary key

memberid medium int unsigned not null

selection tinyint unsigned not null

characteristics tinyint unsigned not null

 

there are about 1.5 million records and indexes on each of the columns

 

querying this table is very fast but where i am having a little trouble is when i want to compare the table to itself.

 

an example of what i am trying to achieve is to list a memberid and if he made the same selection 2 times in a row

 

the way i have done this is to create a new table prevdata

 

recordid varchar(20) not null primary key

prevrecordid varchar(20)

 

prevrecordid has a unique index on it and does contain null values (as sometimes there is no previous information)

 

and then i run the following query

 

select count(*) from records join prevdata on records.recordid=prevdata.recordid

join records as records2 on prevdata.prevrecordid=records2.recordid

where records.selection=records2.selection

 

the query does run but it takes well over a minute which is no good for the environment it will be used in. am i making a design mistake here

 

Link to comment
Share on other sites

Because there is no way of joining it to itself and ensuring accuracy that i can see.

 

How could i tell mysql which row of data is the previous record for any other row without the new table?

 

Its the same principle a lot of queires i now have to build. For example

 

select members with same selection on 4 subsequent occasions

select most common characteristics of members with selection 1 on two of last 3 occasions

 

what adds to the confusion is the same member could and should appear twice if for example he had made the same selection on 4 subsequent occasions more than once

 

there is another table called masterrecord which records general data and i used this to create the prevrecord table

 

masterrecordid mediumint unsigned not null primary key

masterdate date unsigned not null

daybatch tinyint unsigned not null

entrylevel tinyint unsigned not null

owner mediumint unsigned not null

 

this has about 150k rows and again this is indexed and performs very well individually and also when combined with records

 

the only other option i can see is to do self joins based on the two tables using a combination of the masterdate fields to get last occurences. i tried this previously though and it took forever

 

appreciate your time and help

Link to comment
Share on other sites

what i mean is there were several different people gathering data and as a result when the info was loaded there were files of say 2003 ready before 2001 as an example

 

anyway, do you feel the tables are worth rebuilding and if i did how would i write a self join on timestamp. appreciate your help

Link to comment
Share on other sites

what i mean is there were several different people gathering data and as a result when the info was loaded there were files of say 2003 ready before 2001 as an example

 

anyway, do you feel the tables are worth rebuilding and if i did how would i write a self join on timestamp. appreciate your help

I guess I'm trying to figure out exactly what you're trying to query....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.