How to optemise the query having group by on two colomun

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpgurullc
    New Member
    • Aug 2009
    • 3

    How to optemise the query having group by on two colomun

    i have one table say A

    Code:
    +------------+--------------+----------------+
    | value1 | value 2 | value 3 |
    +------------+--------------+----------------+
    | 5 | 4adb83c1d71b | 1 |
    | 5 | 44fcb93589fe | 2 |
    | 5 | 4182b6e2100c | 1 |
    
    | 6 | 4bfd8db46ab1 | 1 |
    
    | 7 | 4b0f87c68808 | 1 |
    | 7 | 413fa4646dfe | 2 |
    | 7 | 4585b5671b68 | 3 |
    | 7 | 4486abc0ddce | 2 |
    | 7 | 4b01b048f07c | 5 |
    | 7 | 4cebb754ff21 | 2 |
    +------------+--------------+----------------+
    Now i need an optemise query that will give number of duplicate in value 3 colomun for each value 1 colomun

    mean i need a result as

    +
    Code:
    ------------+--------------+----------------+
    | value1 | value 3 | count |
    +------------+--------------+----------------+
    | 5 | 1 | 2 |
    | 6 | 1 | 1 |
    | 7 | 2 | 3 |
    +------------+--------------+----------------+

    mean
    Code:
    value 1(5) have value 3(1) repeate 2 time
    value 1(6) have value 3(1) repeate 1 time
    value 1(7) have value 3(2) repeate 3 time
    for this i write a query but that query is not optemise so if have any better solution for query.

    Code:
    select value1,value3,count(value1) as ct 
    from A 
    where value1>0 and value1 != '' 
    group by value1,value3 having ct >1 limit 3;
    where both the colomun value 1 and value 3 are indexed
    Last edited by Atli; Mar 23 '10, 07:49 PM. Reason: Added [code] tags.
Working...