Ticket #2619: sql_query_optimization.diff

File sql_query_optimization.diff, 9.0 kB (added by kanru, 4 months ago)
  • plugins_base/Logger.py

    old new  
    233233        '''get the closest timestamp for a nick change, return None 
    234234        if no stamp is available''' 
    235235 
    236         result = self.query('select e.stamp \ 
    237         from event e, user_event ue, user u \ 
    238         where ue.id_user = u.id and \ 
    239         ue.id_event = e.id and \ 
    240         e.stamp < %s and \ 
    241         u.account = "%s" and \ 
    242         e.name = "nick-changed" \ 
    243         order by e.stamp desc limit 1' % (timestamp, contact)) 
     236        query = ''' 
     237        select e.stamp 
     238        from event as e, (select id_event from user_event where 
     239        id_user=(select id from user where account='%s')) as ue 
     240        where e.id=ue.id_event and 
     241        e.name='nick-changed' and 
     242        e.stamp < %s 
     243        order by stamp desc 
     244        limit 1 
     245        ''' 
     246        result = self.query(query % (contact, timestamp)) 
    244247 
    245248        if len(result) == 0: 
    246249            return None 
     
    254257        one, when we get to that stamp, we get the new nickname, 
    255258        return None if no result''' 
    256259 
    257         result = self.query('select e.stamp \ 
    258         from event e, user_event ue, user u \ 
    259         where ue.id_user = u.id and \ 
    260         ue.id_event = e.id and \ 
    261         e.stamp >= %s and \ 
    262         u.account = "%s" and \ 
    263         e.name = "nick-changed" limit 1' % (timestamp, contact)) 
     260        query = ''' 
     261        select e.stamp 
     262        from event as e, (select id_event from user_event where 
     263        id_user=(select id from user where account='%s')) as ue 
     264        where e.id=ue.id_event and 
     265        e.name='nick-changed' and 
     266        e.stamp >= %s 
     267        order by stamp desc 
     268        limit 1 
     269        ''' 
     270        result = self.query(query % (contact, timestamp)) 
    264271 
    265272        if len(result) == 0: 
    266273            return None 
     
    277284        if stamp is None: 
    278285            return contact 
    279286 
    280         return self.query('select ue.data \ 
    281         from user_event ue, event e \ 
    282         where e.stamp = %f and \ 
    283         ue.id_event = e.id' % (stamp,))[0][0] 
     287        query = ''' 
     288        select ue.data 
     289        from user_event ue, (select id from event where stamp = %f) as e 
     290        where ue.id_event = e.id limit 1 
     291        ''' 
     292 
     293        return self.query(query % (stamp,))[0][0] 
    284294     
    285295    def get_conversation_ids(self, account): 
    286296        '''return the ids of the conversations where the contact was present''' 
    287297         
    288         query = '''select distinct c.id, c.started  
    289         from conversation c, event e, conversation_event ce, user u  
    290         where c.id = ce.id_conversation and  
    291         e.id = ce.id_event and  
    292         ce.id_user = u.id and  
    293         u.account = "%s" 
     298        query = ''' 
     299        select distinct c.id, c.started 
     300        from conversation c, event e, (select id_conversation,id_event from 
     301        conversation_event where id_user = (select id from user 
     302        where account = "%s")) as ce 
     303        where c.id = ce.id_conversation and 
     304        e.id = ce.id_event 
    294305        order by c.started''' 
    295306 
    296307        return self.query(query % (account,)) 
     
    300311        account was present, the format of the list is a list of lists 
    301312        with the timestamp, email and message''' 
    302313 
    303         query = '''select e.stamp, u.account, ce.data 
    304         from event e, conversation_event ce, user u  
    305         where e.id = ce.id_event and  
    306         u.id = ce.id_user and  
    307         ce.id_conversation = %s and 
    308         e.name = "message"  
     314        query = ''' 
     315        select e.stamp, u.account, ce.data 
     316        from event e, user u, (select id_event,id_user,data 
     317        from conversation_event where id_conversation = %s) as ce 
     318        where e.id = ce.id_event and 
     319        e.name = "message" and 
     320        u.id = ce.id_user 
    309321        limit %s''' 
    310322 
    311323        return self.query(query % (id_conversation, num)) 
     
    478490        a list of lists with the timestamp and the status''' 
    479491 
    480492        query = ''' 
    481         select e.stamp, ue.data  
    482         from event e, user_event ue, user u 
    483         where e.id = ue.id_event and 
    484         u.id = ue.id_user and 
    485         e.name = "status-changed" and 
    486         u.account = "%s" 
    487         order by e.stamp desc 
     493        select e.stamp, ue.data 
     494        from event as e, (select id_event,data from user_event where 
     495        id_user=(select id from user where account='%s')) as ue 
     496        where e.id=ue.id_event and 
     497        e.name='status-changed' 
     498        order by stamp desc 
    488499        limit %s 
    489500        ''' 
    490501 
     
    496507        a list of lists with the timestamp and the nick''' 
    497508 
    498509        query = ''' 
    499         select e.stamp, ue.data  
    500         from event e, user_event ue, user u 
    501         where e.id = ue.id_event and 
    502         u.id = ue.id_user and 
    503         e.name = "nick-changed" and 
    504         u.account = "%s" 
    505         order by e.stamp desc 
     510        select e.stamp, ue.data 
     511        from event as e, (select id_event,data from user_event where 
     512        id_user=(select id from user where account='%s')) as ue 
     513        where e.id=ue.id_event and 
     514        e.name='nick-changed' 
     515        order by stamp desc 
    506516        limit %s 
    507517        ''' 
    508518 
     
    514524        a list of lists with the timestamp and the pm''' 
    515525 
    516526        query = ''' 
    517         select e.stamp, ue.data  
    518         from event e, user_event ue, user u 
    519         where e.id = ue.id_event and 
    520         u.id = ue.id_user and 
    521         e.name = "personal-message-changed" and 
    522         u.account = "%s" 
    523         order by e.stamp desc 
     527        select e.stamp, ue.data 
     528        from event as e, (select id_event,data from user_event where 
     529        id_user=(select id from user where account='%s')) as ue 
     530        where e.id=ue.id_event and 
     531        e.name='personal-message-changed' 
     532        order by stamp desc 
    524533        limit %s 
    525534        ''' 
    526535 
     
    565574        a list of lists with the timestamp and the message''' 
    566575 
    567576        query = ''' 
    568         select e.stamp, ce.data  
    569         from event e, conversation_event ce, user u 
    570         where e.id = ce.id_event and 
    571         u.id = ce.id_user and 
    572         e.name = "message" and 
    573         u.account = "%s" 
    574         order by e.stamp desc 
     577        select e.stamp, ce.data 
     578        from event as e, (select id_event,data from conversation_event where 
     579        id_user=(select id from user where account='%s')) as ce 
     580        where e.id=ue.id_event and 
     581        e.name='message' 
     582        order by stamp desc 
    575583        limit %s 
    576584        ''' 
    577585 
     
    583591        a list of lists with the timestamp and the dp''' 
    584592 
    585593        query = ''' 
    586         select e.stamp, ue.data  
    587         from event e, user_event ue, user u 
    588         where e.id = ue.id_event and 
    589         u.id = ue.id_user and 
    590         e.name = "display-picture" and 
    591         u.account = "%s" 
    592         order by e.stamp desc 
     594        select e.stamp, ue.data 
     595        from event as e, (select id_event,data from user_event where 
     596        id_user=(select id from user where account='%s')) as ue 
     597        where e.id=ue.id_event and 
     598        e.name='display-picture' 
     599        order by stamp desc 
    593600        limit %s 
    594601        ''' 
    595602 
     
    601608        a list of lists with the timestamp and the ce''' 
    602609 
    603610        query = ''' 
    604         select e.stamp, ce.data  
    605         from event e, conversation_event ce, user u 
    606         where e.id = ce.id_event and 
    607         u.id = ce.id_user and 
    608         e.name = "custom-emoticon" and 
    609         u.account = "%s" 
    610         order by e.stamp desc 
     611        select e.stamp, ce.data 
     612        from event as e, (select id_event,data from conversation_event where 
     613        id_user=(select id from user where account='%s')) as ce 
     614        where e.id=ue.id_event and 
     615        e.name='custom-emoticon' 
     616        order by stamp desc 
    611617        limit %s 
    612618        ''' 
    613619 
     
    618624        account was present, the format of the list is a list od lists 
    619625        with the timestamp, email and message''' 
    620626 
    621         # in the inner select we select all the id of conversations 
    622         # where account was present, then on the outer conversation 
    623         # we select all the messages from that conversations 
    624627        query = ''' 
    625628        select e.stamp, u.account, ce.data 
    626         from event e, conversation_event ce, user u  
    627         where e.id = ce.id_event and  
    628         u.id = ce.id_user and  
    629         ce.id_conversation in  
    630         (select distinct c.id  
    631         from conversation c, event e, conversation_event ce, user u  
    632         where c.id = ce.id_conversation and  
    633         e.id = ce.id_event and  
    634         ce.id_user = u.id and  
    635         u.account = "%s") and  
    636         e.name = "message"  
     629        from event e, user u, 
     630        (select id_event,id_user,id_conversation,data from 
     631        conversation_event where id_user = (select id from 
     632        user where account = "%s")) as ce 
     633        where e.name = "message" and 
     634        e.id = ce.id_event and 
     635        u.id = ce.id_user 
    637636        order by e.stamp desc 
    638637        limit %s 
    639638        '''