mysql query optimization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hoogendi@cecs.pdx.edu

    #1

    mysql query optimization

    I'm having some problems optimizing this query:

    select distinct l2.* from linetmp l1, linetmp l2 where
    l1.fname = ""
    and left(l1.id,3)<> "SUB"
    and l2.fname <> ""
    and (l1.n1 = l2.n1 or l1.n1 = l2.n2 or l1.n2 = l2.n1 or l1.n2 = l2.n2)

    when i use explain on this query i get:
    id, select_type, table, type, possible_keys, key, key_len, ref, rows,
    Extra
    1, 'SIMPLE', 'l1', 'ref', 'Index_1,Index_ 2,Index_4', 'Index_4', 45,
    'const', 11348, 'Using where; Using temporary'
    1, 'SIMPLE', 'l2', 'ALL', 'Index_1,Index_ 2,Index_4', '', , '', 363890,
    'Using where; Distinct'

    Here is the table information:

    `feeder` decimal(10,0) NOT NULL default '0',
    `fname` varchar(45) NOT NULL default '',
    `c1` varchar(45) NOT NULL default '',
    `cp` bigint(10) unsigned default NULL,
    `fpos` bigint(20) unsigned NOT NULL default '0',
    `c2` varchar(45) NOT NULL default '',
    `n1` decimal(10,0) NOT NULL default '0',
    `n2` decimal(10,0) NOT NULL default '0',
    `OU` bigint(1) unsigned NOT NULL default '0',
    `length` decimal(10,3) NOT NULL default '0.000',
    `ltfpos` bigint(10) unsigned NOT NULL default '0',
    `d1` bigint(10) unsigned NOT NULL default '0',
    `d2` bigint(10) unsigned NOT NULL default '0',
    `id` varchar(45) NOT NULL default '',
    KEY `Index_1` (`n1`),
    KEY `Index_2` (`n2`),
    KEY `Index_3` (`fpos`),
    KEY `Index_4` (`fname`)

    the table has 363890 records.

    When i run this query:
    select distinct l2.* from linetmp l1, linetmp l2 where
    l1.fname = ""
    and left(l1.id,3)<> "SUB"
    and l2.fname <> ""
    and l1.n1 = l2.n1

    It works perfectly. Actually, I can union 4 queries and get what I am
    after, but that just tells me that there is a better way to do it.

    When i use explain on this i get:
    id, select_type, table, type, possible_keys, key, key_len, ref, rows,
    Extra
    1, 'SIMPLE', 'l1', 'ref', 'Index_1,Index_ 4', 'Index_4', 45, 'const',
    11348, 'Using where; Using temporary'
    1, 'SIMPLE', 'l2', 'ref', 'Index_1,Index_ 4', 'Index_1', 11,
    'pcc.l1.n1', 1, 'Using where'

    I've tried using force index to force the use of index_1 and index_2 on
    l2. I've also tried forcing the use of index_1 and index_2 on both l1
    and l2, and also just on l1. I'm not sure what else I can do. Anybody
    tips would be greatly appreciated.

Working...