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 233 233 '''get the closest timestamp for a nick change, return None 234 234 if no stamp is available''' 235 235 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)) 244 247 245 248 if len(result) == 0: 246 249 return None … … 254 257 one, when we get to that stamp, we get the new nickname, 255 258 return None if no result''' 256 259 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)) 264 271 265 272 if len(result) == 0: 266 273 return None … … 277 284 if stamp is None: 278 285 return contact 279 286 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] 284 294 285 295 def get_conversation_ids(self, account): 286 296 '''return the ids of the conversations where the contact was present''' 287 297 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 294 305 order by c.started''' 295 306 296 307 return self.query(query % (account,)) … … 300 311 account was present, the format of the list is a list of lists 301 312 with the timestamp, email and message''' 302 313 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 309 321 limit %s''' 310 322 311 323 return self.query(query % (id_conversation, num)) … … 478 490 a list of lists with the timestamp and the status''' 479 491 480 492 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 488 499 limit %s 489 500 ''' 490 501 … … 496 507 a list of lists with the timestamp and the nick''' 497 508 498 509 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 506 516 limit %s 507 517 ''' 508 518 … … 514 524 a list of lists with the timestamp and the pm''' 515 525 516 526 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 524 533 limit %s 525 534 ''' 526 535 … … 565 574 a list of lists with the timestamp and the message''' 566 575 567 576 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 575 583 limit %s 576 584 ''' 577 585 … … 583 591 a list of lists with the timestamp and the dp''' 584 592 585 593 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 593 600 limit %s 594 601 ''' 595 602 … … 601 608 a list of lists with the timestamp and the ce''' 602 609 603 610 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 611 617 limit %s 612 618 ''' 613 619 … … 618 624 account was present, the format of the list is a list od lists 619 625 with the timestamp, email and message''' 620 626 621 # in the inner select we select all the id of conversations622 # where account was present, then on the outer conversation623 # we select all the messages from that conversations624 627 query = ''' 625 628 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 637 636 order by e.stamp desc 638 637 limit %s 639 638 '''
