0

I've been tackling this for a long time and I've decided I will ask to see if someone can suggest something before my head explodes.

Here is the query I'm running, it's just repeating the same sub-sub-query multiple times within the sub-queries. I wanted to see what ALTERNATIVE options I have to speed this up.

All I can think so far is running an initial query then inserting the results for a second query.

Thanks in advance for anyones time looking at this.

SELECT t2.* FROM TABLEA as t2 where
t2.link_id in 
(
    select t4.LinkA as link_list from TABLEB as t4
    where t4.LinkB in (
        select t1.id from TABLEC as t1 where t1.number in
        (
            (select t2.number from TABLEC as t2 where t2.id in 
                (
                select t3.link_id from TABLEA as t3 where t3.an like "%20278%"
                )
            )
        )
    )
    union

    select t4.LinkB as link_list from TABLEB as t4
    where t4.LinkB in (
        select t1.id from TABLEC as t1 where t1.part_number in
        (
            (select t2.number from TABLEC as t2 where t2.id in 
                (
                select t3.link_id from TABLEA as t3 where t3.an like "%20278%"
                )
            )
        )
    )
    union


    select t4.LinkA as link_list from TABLEB as t4
    where t4.LinkA in (
        select t1.id from TABLEC as t1 where t1.number in
        (
            (select t2.number from TABLEC as t2 where t2.id in 
                (
                select t3.link_id from TABLEA as t3 where t3.an like "%20278%"
                )
            )
        )
    )
    union

    select t4.LinkB as link_idlist from TABLEB as t4
    where t4.LinkA in (
        select t1.id from TABLEC as t1 where t1.number in
        (
            (select t2.number from TABLEC as t2 where t2.id in 
                (
                select t3.link_id from TABLEA as t3 where t3.an like "%20278%"
                )
            )
        )
    )
)
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Sidupac
  • 651
  • 7
  • 11
  • `select *` with `union` is suspicious, particularly when used with specific columns from other tables. – Gordon Linoff Jul 20 '19 at 02:12
  • I'm not sure what you mean by suspicious? – Sidupac Jul 25 '19 at 01:00
  • . . The code does not look correct and would not work in most databases, including more recent versions of MySQL with default settings. – Gordon Linoff Jul 25 '19 at 02:21
  • It was working as expected on 10.1.38-MariaDB-0+deb9u1 - Debian 9.8 I'm pretty sure I changed the code to work in a slightly different way in the end, so this question is currently irrelevant – Sidupac Aug 12 '19 at 02:30

0 Answers0