# Python-sql In some cases this results in hundreds of thousands of calls to the \_\_str\_\_ loop

**URL:** <https://discuss.tryton.org/t/python-sql-in-some-cases-this-results-in-hundreds-of-thousands-of-calls-to-the-str-loop/4169>\
**Category:** Developer\
**Tags:** python-sql\
**Created:** [May 9, 2021, 5:32am UTC](https://discuss.tryton.org/t/python-sql-in-some-cases-this-results-in-hundreds-of-thousands-of-calls-to-the-str-loop/4169 "2021-05-09T05:32:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhuo.wang](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/z/5daacb/32.png) [@zhuo.wang](https://discuss.tryton.org/u/zhuo.wang)\
**Post date:** [May 9, 2021, 5:32am UTC](https://discuss.tryton.org/t/python-sql-in-some-cases-this-results-in-hundreds-of-thousands-of-calls-to-the-str-loop/4169/1 "2021-05-09T05:32:41Z")

</div>

### Hai, I’m using python-sql in python2.7 windows now，In some cases, parsing SQL is getting slow.

- I generated pickle objects from SQL objects, and I noticed that the **str** method parsing SQL seems to be called in a loop. An SQL parse will call the function **str** hundreds of thousands of times, which will take several seconds
- python 2.7 python-sql==1.2.1
- Here is my test file  
[http://www.chaoyue.red/static/select.pkl](http://www.chaoyue.red/static/select.pkl)

```auto
import pickle
import sql
select = pickle.load(open('select.pkl','rb'))
%time tuple(select)

```

 ![test](https://discuss-cdn.tryton.org/uploads/default/original/2X/4/4c6c62b977bea6d21dd27bdb0e6b7271ab69bc79.png)

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [May 9, 2021, 7:39am UTC](https://discuss.tryton.org/t/python-sql-in-some-cases-this-results-in-hundreds-of-thousands-of-calls-to-the-str-loop/4169/2 "2021-05-09T07:39:12Z")

</div>

Depending of the size, number of parameters etc. of the query, some methods may be called multiple times. I do not understand the problem.

---

<div class="post-metadata">

**Author:** ![zhuo.wang](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/z/5daacb/32.png) [@zhuo.wang](https://discuss.tryton.org/u/zhuo.wang)\
**Post date:** [May 9, 2021, 8:23am UTC](https://discuss.tryton.org/t/python-sql-in-some-cases-this-results-in-hundreds-of-thousands-of-calls-to-the-str-loop/4169/3 "2021-05-09T08:23:19Z")

</div>

I found that this function was called hundreds of thousands of times,The generated SQL is duplicated,Then I changed this part of the code

```python
# sql line 1053
    def __str__ (self):
        join = '%s %s JOIN %s' % (From([self.left]), self.type_,
            From([self.right]))
        if self.condition:
            condition = ' ON %s' % self.condition
        else:
            condition = ''
        return join + condition

```

```python
import sql
from sql import From
from threading import Lock
lock = Lock()
_only = {}

sql.lock = lock
sql._only = _only

def __iter__ (self):
    with lock:
        _only.clear()
        yield str(self)
        yield self.params

def __str__ (self):
    query = (self.left, self.type_, self.right)
    if (query) in _only:
        return _only[query]
    else:
        join = '%s %s JOIN %s' % (From([self.left]), self.type_,
        From([self.right]))
        if self.condition:
            condition = ' ON %s' % self.condition
        else:
            condition = ''
        _only[query] = join + condition
        return join + condition

sql.Query. __iter__ = __iter__
sql.Join. __str__ = __str__

```

- Performed again

 ![test3](https://discuss-cdn.tryton.org/uploads/default/original/2X/7/74fcb399ad96657221c4ab246b0da141f973997a.png)

The number of loops is reduced, and the results are consistent

Confused as to why repeated join SQL calls multiple times？

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [May 9, 2021, 9:32am UTC](https://discuss.tryton.org/t/python-sql-in-some-cases-this-results-in-hundreds-of-thousands-of-calls-to-the-str-loop/4169/4 "2021-05-09T09:32:44Z")

</div>

I do not see why there should be multiple call to ` __str__ ` of `Join`. I’m wondering if it is not your Python shell that triggers those calls.
