Showing posts with label emergency. Show all posts
Showing posts with label emergency. Show all posts

Friday, March 23, 2012

Need Emergency Help with SQL Query (difficulty: moderate)

Panicked, as usual. Who says it's great being a sole proprietor?

Using MS-SQLServer:

Three tables: A_Appointment, AX_Appointment_Entity and E_Entity.
AX_Appointment_Entity is an intersect/association table between
A_Appointment and E_Entity. One appointment may have many attendees
(Appointment_Entity). One attendee (Entity) may have many
appointments.

My task is to delete all appointments and related
AX_Appointment_Entity rows where the number of attendees is one or
less.

I'm great at standard select or delete queries. Because this straddles
tables in a way I'm less skilled in, I'm pleading for help.

Thanks for any ideas. fwiw, I'm using Transact-SQL.

BLink
--------
"The worst thing about censorship is [redacted]"Brian Link wrote:

Quote:

Originally Posted by

Three tables: A_Appointment, AX_Appointment_Entity and E_Entity.
AX_Appointment_Entity is an intersect/association table between
A_Appointment and E_Entity. One appointment may have many attendees
(Appointment_Entity). One attendee (Entity) may have many
appointments.
>
My task is to delete all appointments and related
AX_Appointment_Entity rows where the number of attendees is one or
less.


If deletions from A_Appointment cascade to AX_AppointmentEntry:

delete from A_Appointment
where AppointmentKey in (
select AppointmentKey
from AX_Appointment_Entity
group by AppointmentKey
having count(*) <= 1
)

If deletions from A_Appointment are blocked until the matches in
AX_Appointment_Entity are also deleted:

select AppointmentKey
into #appointments_to_delete
from AX_Appointment_Entity
group by AppointmentKey
having count(*) <= 1

delete from AX_Appointment_Entity
where AppointmentKey in (
select AppointmentKey
from #appointments_to_delete
)

delete from A_Appointments
where AppointmentKey in (
select AppointmentKey
from #appointments_to_delete
)

If deletions from A_Appointment leave orphans in AX_Appointment_Entity
(why?):

delete from A_Appointment
where AppointmentKey in (
select AppointmentKey
from AX_Appointment_Entity
group by AppointmentKey
having count(*) <= 1
)

delete from Ax_Appointment_Entity
where AppointmentKey in (
select AppointmentKey
from AX_Appointment_Entity
group by AppointmentKey
having count(*) <= 1
)|||Here is one solution:

Delete Appointment a
Where exists (select 'hi there'
From Entity e
Where a.attendee = e. attendee
Group by e. attendee
Having count(e. attendee) <= 1)

You can do the similar step with the intersect table
Appointment_Entity.

Brian Link wrote:

Quote:

Originally Posted by

Panicked, as usual. Who says it's great being a sole proprietor?
>
Using MS-SQLServer:
>
Three tables: A_Appointment, AX_Appointment_Entity and E_Entity.
AX_Appointment_Entity is an intersect/association table between
A_Appointment and E_Entity. One appointment may have many attendees
(Appointment_Entity). One attendee (Entity) may have many
appointments.
>
My task is to delete all appointments and related
AX_Appointment_Entity rows where the number of attendees is one or
less.
>
I'm great at standard select or delete queries. Because this straddles
tables in a way I'm less skilled in, I'm pleading for help.
>
Thanks for any ideas. fwiw, I'm using Transact-SQL.
>
BLink
--------
"The worst thing about censorship is [redacted]"

|||othellomy@.yahoo.com wrote:

Quote:

Originally Posted by

Delete Appointment a
Where exists (select 'hi there'
From Entity e
Where a.attendee = e. attendee
Group by e. attendee
Having count(e. attendee) <= 1)


A_Appointment doesn't have an 'attendee' column. Original post: "One
appointment may have multiple attendees (Appointment_Entity)".

Reworking this to use AX_Appointment_Entity (and to change 'delete' to
'delete from') ought to lead to another valid solution.|||Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Guessing at what you meant to post, and using data
element names that make sense, does your schema look like this?

CREATE TABLE Appointments
(appt_id INTEGER NOT NULL PRIMARY KEY,
appt_date DATETIME NOT NULL,
...);

CREATE TABLE Attendees
(attendee_id INTEGER NOT NULL PRIMARY KEY,
attendee_name VARCHAR(20) NOT NULL,
..);

CREATE TABLE Attendance
(appt_id INTEGER NOT NULL
REFERENCES Appointments (appt_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
attendee_id INTEGER NOT NULL
REFERENCES Attendees (attendee_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
PRIMARY KEY (appt_id, attendee_id));

1)You never use "-key" as a suffix. That tells us HOW the data
element is used and not WHAT the data element is. Likewise, you never
put silly prefixes that tell WHERE there is an appearance or what the
data type used is.

2)The name "entity" is too vague to be data element name. Do you
also use "thingie" or "Whatyamacallit" in your data dictionary?
Use particular names, not vague ones. If you cannot thik of a good
name, then you do not have a clear idea of what you are doing yet.

3)A table models a set of like entities and therefore should have a
collective or plural name. Unless they really do model one thing.

4)The correct term is a relationship table, in this case a
many-to-many relationship. There is not such thing as an intersection
or association table.

5)Notice that you do most of your work with DRI actions, not in code.
The idea of a RDBMS is that it keeps data integrity for you.

Quote:

Originally Posted by

Quote:

Originally Posted by

>My task is to delete all appointments and related Attendance rows where the number of attendees is one or less. <<


DELETE FROM Appointments
WHERE appt_id
IN (SELECT A1.appt_id
FROM Attendance AS A1
GROUP BY A1.appt_id
HAVING COUNT(*) <= 1);

The DRI makes < 1 redundant, but it preserves what you posted. The DRI
actions will re-arrange the Attendance as people or appointments drop
out.|||--CELKO-- wrote:

Quote:

Originally Posted by

1)You never use "-key" as a suffix. That tells us HOW the data
element is used and not WHAT the data element is.


I work with an established package that uses "-key" as a suffix
regularly, but always for artificial keys (so "what the data element
is" is established across the board by convention as "nothing the
end user has any business seeing").

Quote:

Originally Posted by

5)Notice that you do most of your work with DRI actions, not in code.
The idea of a RDBMS is that it keeps data integrity for you.


[snip]

Quote:

Originally Posted by

The DRI makes < 1 redundant, but it preserves what you posted. The DRI
actions will re-arrange the Attendance as people or appointments drop
out.


If they drop out completely. If a person drops one appointment but
retains others, then the system should directly delete the Attendance
row corresponding to the dropped person+appointment pair.|||You have three possible actions which should be in separate procedures

CREATE PROCEDURE DelAppointment (@.myappt_id INTEGER)
DELETE FROM Appointments -- meeting cancelled
WHERE appt_id = @.my_appt_id;

CREATE PROCEDURE DelAttendee(@.my_attendee_id INTEGER)
DELETE FROM Attendees -- employee gone
WHERE attendee_id = @.my_attendee_id;

CREATE PROCEDURE DelAttendance
(@.my_attendee_id INTEGER , @.myappt_id INTEGER)
BEGIN
DELETE FROM Attendance -- employee removed from meeting
WHERE appt_id = @.my_appt_id
AND attendee_id = @.my_attendee_id;
-- if the was the next to last guy, cancel the meeting
DELETE FROM Appointments
WHERE appt_id
IN (SELECT A1.appt_id
FROM Attendance AS A1
GROUP BY A1.appt_id
HAVING COUNT(*) <= 1);
END;

CREATE PROCEDURE
(@.my_attendee_id INTEGER , @.myappt_id INTEGER)

Watch your error handling and transaction level in the last procedure.

Need Emergency Advice

Dear All,

We have a big concern in our Database system. We have 2000 transactions
daily in our database. We need to replicate some how the database for
our fail over setup. I tried transactional replication at midnight but
our all systems locked and we had a lot of complaints from the
customers and It was taking a lot of time to snapshot part and I had to
abort it because of these reasons.
I need an advice how I can create a replication or is there any other
way to replicate or is there any way to do this process without
bothering the system? Please give me some advice and help..

Thanks
asa."laststubborn" <arafatsalih@.gmail.com> wrote in message
news:1147164897.180455.80350@.j33g2000cwa.googlegro ups.com...
> Dear All,
> We have a big concern in our Database system. We have 2000 transactions
> daily in our database. We need to replicate some how the database for
> our fail over setup. I tried transactional replication at midnight but
> our all systems locked and we had a lot of complaints from the
> customers and It was taking a lot of time to snapshot part and I had to
> abort it because of these reasons.

Why do the transactional replication only at midnight? Better off doing it
continually throughout the day.

And btw, 2000 transactions, unless they're huge, is trivial. I have systems
doing that per minute.

The other option is to look into log shipping.

There are scripts out there that let you do it even w/o Enterprise edition,
though it is then admittedly more manual.

> I need an advice how I can create a replication or is there any other
> way to replicate or is there any way to do this process without
> bothering the system? Please give me some advice and help..
> Thanks
> asa.|||Greg D. Moore (Strider) wrote:
> "laststubborn" <arafatsalih@.gmail.com> wrote in message
> news:1147164897.180455.80350@.j33g2000cwa.googlegro ups.com...
> > Dear All,
> > We have a big concern in our Database system. We have 2000 transactions
> > daily in our database. We need to replicate some how the database for
> > our fail over setup. I tried transactional replication at midnight but
> > our all systems locked and we had a lot of complaints from the
> > customers and It was taking a lot of time to snapshot part and I had to
> > abort it because of these reasons.
> Why do the transactional replication only at midnight? Better off doing it
> continually throughout the day.

The thing is snapshot taking a lot of time to do it. Even at night we
had a problem with customers.

> And btw, 2000 transactions, unless they're huge, is trivial. I have systems
> doing that per minute.
> The other option is to look into log shipping.
> There are scripts out there that let you do it even w/o Enterprise edition,
> though it is then admittedly more manual.

As you said it is manual we can not afford manual solutions.

Thanks

>
> > I need an advice how I can create a replication or is there any other
> > way to replicate or is there any way to do this process without
> > bothering the system? Please give me some advice and help..
> > Thanks
> > asa.|||"laststubborn" <arafatsalih@.gmail.com> wrote in message
news:1147185632.084129.202140@.j73g2000cwa.googlegr oups.com...
> Greg D. Moore (Strider) wrote:
> > "laststubborn" <arafatsalih@.gmail.com> wrote in message
> > news:1147164897.180455.80350@.j33g2000cwa.googlegro ups.com...
> > > Dear All,
> > > > We have a big concern in our Database system. We have 2000
transactions
> > > daily in our database. We need to replicate some how the database for
> > > our fail over setup. I tried transactional replication at midnight but
> > > our all systems locked and we had a lot of complaints from the
> > > customers and It was taking a lot of time to snapshot part and I had
to
> > > abort it because of these reasons.
> > Why do the transactional replication only at midnight? Better off doing
it
> > continually throughout the day.
> The thing is snapshot taking a lot of time to do it. Even at night we
> had a problem with customers.

I'm not following, or we're not communicating effectively. A transactional
backup should not be taking that long or have that much impact. How large
are these transactions? Like I say, I have a system doing that many a
minute (though that one we don't backup). The one we do transactional
backups on every 20 minutes probably does 2000/hour or more at peak time.
(plus a very high number of pure select only queries.)

> > And btw, 2000 transactions, unless they're huge, is trivial. I have
systems
> > doing that per minute.
> > The other option is to look into log shipping.
> > There are scripts out there that let you do it even w/o Enterprise
edition,
> > though it is then admittedly more manual.
> As you said it is manual we can not afford manual solutions.

Well, what do you consider too manual. In the case of many of these scripts
the failover itself may or may not be automatic, but can generally be
scripted. The log-shipping itself is scripted.

In our case, we do log backups from our main server every 20 minutes and
restore them to our backup server every 20 minutes, but with either a 4 hour
or 6 hour delay (I forget which). To bring the backup system "up to date" I
can do in about 15 minutes.. either manually or via running a quick script.

The bigger delay (whcih we'd have using Enterprise Server also) is updating
our front end DSNs. And we have that scripted also.

All in all, a failover would probably take us 15 minutes or less to recover
from.
> Thanks
> > > I need an advice how I can create a replication or is there any other
> > > way to replicate or is there any way to do this process without
> > > bothering the system? Please give me some advice and help..
> > > > Thanks
> > > asa.
>|||yep I think we had a communication problem. I was asking about the
replication not for a backup (I dont know whether you mean replication
when you use 'transactional backup') Back Up is not a problem as you
said we dont have problem on that however I will create failover
cluster but I will replicate the database first. That is my problem
transactional replication.. That is why I asked all these questions.

Thank you|||"laststubborn" <arafatsalih@.gmail.com> wrote in message
news:1147244663.232015.249020@.u72g2000cwu.googlegr oups.com...
> yep I think we had a communication problem. I was asking about the
> replication not for a backup (I dont know whether you mean replication
> when you use 'transactional backup') Back Up is not a problem as you
> said we dont have problem on that however I will create failover
> cluster but I will replicate the database first. That is my problem
> transactional replication.. That is why I asked all these questions.

Again, I'm still not sure what you mean here.

In my original post I asked why you weren't simply doing the transactional
replication throughout the day.

Then you brought up snap-shotting. This isn't a term usually used with
transactional replication.

A transactional replication setup really shouldn't lock your system, so I'm
missing something here.

> Thank you|||Greg D. Moore (Strider) (mooregr_deleteth1s@.greenms.com) writes:
> Then you brought up snap-shotting. This isn't a term usually used with
> transactional replication.

Not that replication is my best game, but doesn't a transactional
replication scheme usually start with the replication of a snapshot?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97BFF41144A46Yazorman@.127.0.0.1...
> Greg D. Moore (Strider) (mooregr_deleteth1s@.greenms.com) writes:
> > Then you brought up snap-shotting. This isn't a term usually used with
> > transactional replication.
> Not that replication is my best game, but doesn't a transactional
> replication scheme usually start with the replication of a snapshot?

Yes, but it's a one time game. And there are ways around that (not
necessarily clean ones unfortunately).

>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||Thats the thing, I could not pass one time snapshot part. During the
initial snapshot I had problems, Our application was locked and It was
taking so long we had to cancel it. You said that there are ways around
that how ? If you give me some help on that I really appreciate it.

Also any of you did a failover clustering? I will do this first time
and I have some questions.

Thanks|||"laststubborn" <arafatsalih@.gmail.com> wrote in message
news:1147348989.967368.134060@.j73g2000cwa.googlegr oups.com...
> Thats the thing, I could not pass one time snapshot part. During the
> initial snapshot I had problems, Our application was locked and It was
> taking so long we had to cancel it. You said that there are ways around
> that how ? If you give me some help on that I really appreciate it.

Ah, now I better understand what you're talking about.

I'm a bit surpised with only 2000 transactions a day you can't fnid a quiet
time (perhaps on a weekend?) to make the snapshot.

Anyway, there's KB articles on MS on various ways of doing this (and I
believe SQL 2005 has options also that help.)

Anyway, if you CAN pause work for a shrot period of time, try something like
the following:

Do a full backup of DB on "Publisher".
Do a full restore WITH NORECOVERY on "subscriber"
Since this will take some time, you'll need to do the following (and up
until now you users could still use the system.)

Stop all access to the system
Do a transaction log backup from the "publisher"
Restore to the "subscriber", this time with RECOVERY

Setup replication, but say, "subscriber has data and schema"

Permit access to the system.

This is not guaranteed to solve your problem, but I've done it with success
before.

There's several catches to watch out for:

1) if you know your initial transaction log backup will take a long time, do
it w/o kicking folks out of the publisher and restore with NORECOVERY and
then do the transaction log backup a 2nd time (or 3rd, etc.) Trick is to
get that last transaction log backup/restore cycle as absolutely as short as
possible.

Setup as much of replication in advance as possible, i.e. setup the
publisher/distributer side of things before hand.

Script out as much as you can.

Finally.. be careful of IDENTITY columns. This is where things can break.

Note to, if you decide you need to make schema changes on the subscriber, do
this after replication is setup.

The above is not guaranteed to solve your problem but I've used it in
similar cases with success.

> Also any of you did a failover clustering? I will do this first time
> and I have some questions.
> Thanks