[Solved] DB2 how to find two user who are active in the same date

EverSQL Database Performance Knowledge Base

DB2 how to find two user who are active in the same date

I´m trying two find user who were active in the same date, I´ve code that runs

select place,date,user, artNumber

from jobs
where user in (

select user2

from userActive

where date between 20180101 and 20181011 and user2 = 'nightJob')

This just give me when nightJob have been active, how can I match so the result return wich other user have been active in the same time as nightJob, I would love an explanation of the sulotion becuse I´m trying to learn.

I sound like amatur and mabye I´m, but can someone explain my code, I´ve wrot e it but I just whant to check if I understand it correctly. I dont whant fixed results, its over hundred users but nightJob is always that specific username.

desierd result

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `userActive` ADD INDEX `useractive_idx_user2_date` (`user2`,`date`);
The optimized query:
SELECT
        jobs.place,
        jobs.date,
        jobs.user,
        jobs.artNumber 
    FROM
        jobs 
    WHERE
        EXISTS (
            SELECT
                1 
            FROM
                userActive 
            WHERE
                (
                    userActive.date BETWEEN 20180101 AND 20181011 
                    AND userActive.user2 = 'nightJob'
                ) 
                AND (
                    jobs.user = userActive.user2
                )
        )

Related Articles



* original question posted on StackOverflow here.