开发者

How to interface with another database effectively using python

开发者 https://www.devze.com 2023-04-05 04:34 出处:网络
I have an application that needs to interface with another app\'s database. I have read access but not write.

I have an application that needs to interface with another app's database. I have read access but not write.

Currently I'm using sql statements via pyodbc to grab the rows and using python manipulate 开发者_如何学运维the data. Since I don't cache anything this can be quite costly.

I'm thinking of using an ORM to solve my problem. The question is if I use an ORM like "sql alchemy" would it be smart enough to pick up changes in the other database?

E.g. sql alchemy accesses a table and retrieves a row. If that row got modified outside of sql alchemy would it be smart enough to pick it up?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Edit: To be more clear

I have one application that is simply a reporting tool lets call App A.

I have another application that handles various financial transactions called App B.

A has access to B's database to retrieve the transactions and generates various reports. There's hundreds of thousands of transactions. We're currently caching this info manually in python, if we need an updated report we refresh the cache. If we get rid of the cache, the sql queries combined with the calculations becomes unscalable.


I don't think an ORM is the solution to your problem of performance. By default ORMs tend to be less efficient than row SQL because they might fetch data that you're not going to use (eg. doing a SELECT * when you need only one field), although SQLAlchemy allows fine-grained control over the SQL generated.

Now to implement a caching mechanism, depending on your application, you could use a simple dictionary in memory or a specialized system such as memcached or Redis.

To keep your cached data relatively fresh, you can poll the source at regular intervals, which might be OK if your application can tolerate a little delay. Otherwise you'll need the application that has write access to the db to notify your application or your cache system when an update occurs.

Edit: since you seem to have control over app B, and you've already got a cache system in app A, the simplest way to solve your problem is probably to create a callback in app A that app B can call to expire cached items. Both apps need to agree on a convention to identify cached items.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号