Django 更新一条可能不存在的特定数据是 Get 性能好还是 Filter 好 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Phishion
V2EX    Django

Django 更新一条可能不存在的特定数据是 Get 性能好还是 Filter 好

  •  
  •   Phishion 2021-01-26 19:47:17 +08:00 3105 次点击
    这是一个创建于 1729 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想优化一下性能,但是有一个地方有 2 种写法,我不知道哪种好一点,就是比如有条数据进来,我要先判断这条数据在不在,如果在就更新,不在就直接新建一条

    伪代码如下:

    方法 1 )

    try: article = Article.objects.get(id=9999) 触发:更新逻辑 except: 触发:新建逻辑 

    方法 2 )

    article = Article.objects.filter(id=9999) if article.exists(): exist_article = article[0] 触发:更新逻辑 else: 触发:新建逻辑 

    感觉方法 1 有点奇怪,但是似乎就访问一次数据库,方法二嗦一点,不知道有没有老鸟能提供帮助?

    19 条回复    2021-01-27 18:33:37 +08:00
    wuwukai007
        1
    wuwukai007  
       2021-01-26 19:50:13 +08:00
    get_or_create
    youngce
        2
    youngce  
       2021-01-26 19:51:38 +08:00
    Article.objects.update_or_create() ?
    Phishion
        3
    Phishion  
    OP
       2021-01-26 19:51:49 +08:00
    @wuwukai007 请问,这种分装好的底层又是怎么实现的呢?
    renmu123
        4
    renmu123  
       2021-01-26 19:51:58 +08:00 via Android
    1 比较符合 Python 哲学
    Phishion
        5
    Phishion  
    OP
       2021-01-26 19:55:51 +08:00
    @wuwukai007
    我看它文档里面还真是 try 出来的,真原始啊
    zeroDev
        6
    zeroDev  
       2021-01-26 19:55:58 +08:00 via Android
    应该用 try except 结构,避免判断
    参考 Python 官方文档 https://docs.python.org/3.6/glossary.html#term-eafp
    zeroDev
        7
    zeroDev  
       2021-01-26 19:56:54 +08:00 via Android
    @Phishion try 就避免了很多条件判断,写 Python 本身就是为了快速开发嘛
    Phishion
        8
    Phishion  
    OP
       2021-01-26 19:57:23 +08:00
    @zeroDev 好的 我瞧瞧
    Phishion
        9
    Phishion  
    OP
       2021-01-26 19:58:08 +08:00
    @zeroDev 我一直潜意识认为报错会有额外的性能开销,所以就感觉用 try 处理正常业务逻辑很奇怪
    wuwukai007
        10
    wuwukai007  
       2021-01-26 20:04:18 +08:00
    你可以这样理解,如果存在的可能新远远大于 不存在,那么使用 try 的性能不就会搞的多吗,不用 try,每次都要判断
    Phishion
        11
    Phishion  
    OP
       2021-01-26 20:07:05 +08:00
    @wuwukai007 有道理,谢谢!
    Geek981108
        12
    Geek981108  
       2021-01-26 20:07:29 +08:00
    文档如是说:
    update_or_create()
    update_or_create(defaults=None, **kwargs)
    A convenience method for updating an object with the given kwargs, creating a new one if necessary. The defaults is a dictionary of (field, value) pairs used to update the object. The values in defaults can be callables.

    Returns a tuple of (object, created), where object is the created or updated object and created is a boolean specifying whether a new object was created.

    The update_or_create method tries to fetch an object from database based on the given kwargs. If a match is found, it updates the fields passed in the defaults dictionary.

    This is meant as a shortcut to boilerplatish code. For example:

    defaults = {'first_name': 'Bob'}
    try:
    obj = Person.objects.get(first_name='John', last_name='Lennon')
    for key, value in defaults.items():
    setattr(obj, key, value)
    obj.save()
    except Person.DoesNotExist:
    new_values = {'first_name': 'John', 'last_name': 'Lennon'}
    new_values.update(defaults)
    obj = Person(**new_values)
    obj.save()
    This pattern gets quite unwieldy as the number of fields in a model goes up. The above example can be rewritten using update_or_create() like so:

    obj, created = Person.objects.update_or_create(
    first_name='John', last_name='Lennon',
    defaults={'first_name': 'Bob'},
    )
    For detailed description how names passed in kwargs are resolved see get_or_create().

    As described above in get_or_create(), this method is prone to a race-condition which can result in multiple rows being inserted simultaneously if uniqueness is not enforced at the database level.

    Like get_or_create() and create(), if you’re using manually specified primary keys and an object needs to be created but the key already exists in the database, an IntegrityError is raised.
    lixuda
        13
    lixuda  
       2021-01-26 20:10:52 +08:00
    我用这样,不知道性能好不好
    article = Article.objects.filter(id=9999).first()
    if(article):
    Phishion
        14
    Phishion  
    OP
       2021-01-26 20:13:12 +08:00
    @lixuda 我觉得用它封装好的性能最好,判断太多代码也显得不整洁
    lixuda
        15
    lixuda  
       2021-01-26 21:45:31 +08:00
    @Phishion 看错了,是更新逻辑,update_or_create
    Austaras
        16
    Austaras  
       2021-01-27 00:51:32 +08:00
    有的数据库支持 upsert
    chaleaoch
        17
    chaleaoch  
       2021-01-27 09:21:57 +08:00
    你看源码,其实这俩逻辑差不多. get 里面就是 filter + try catch
    nonduality
        18
    nonduality  
       2021-01-27 15:17:55 +08:00
    你的方法 2 会引发两次查询,优化文档经常提到不要滥用 exists():如果后续仍需要使用到对象数据,就无须用 exists()去判断对象存在与否。

    用 #13 的方法也可以。
    Phishion
        19
    Phishion  
    OP
       2021-01-27 18:33:37 +08:00
    @nonduality
    @chaleaoch
    @lixuda
    感谢提供指导!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3261 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 11:50 PVG 19:50 LAX 04:50 JFK 07:50
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86