In what scenarios would I need to use the CREATEREF, DEREF and REF keywords?

放肆的年华 提交于 2019-12-30 11:23:26

问题


This question is about why I would use the above keywords. I've found plenty of MSDN pages that explain how. I'm looking for the why.

What query would I be trying to write that means I need them? I ask because the examples I have found appear to be achievable in other ways...

To try and figure it out myself, I created a very simple entity model using the Employee and EmployeePayHistory tables from the AdventureWorks database.

One example I saw online demonstrated something similar to the following Entity SQL:

SELECT VALUE
    DEREF(CREATEREF(AdventureWorksEntities3.Employee, row(h.EmployeeID))).HireDate
FROM 
    AdventureWorksEntities3.EmployeePayHistory as h

This seems to pull back the HireDate without having to specify a join?

Why is this better than the SQL below (that appears to do exactly the same thing)?

SELECT VALUE
    h.Employee.HireDate
FROM 
    AdventureWorksEntities3.EmployeePayHistory as h

Looking at the above two statements, I can't work out what extra the CREATEREF, DEREF bit is adding since I appear to be able to get at what I want without them.

I'm assuming I have just not found the scenarios that demostrate the purpose. I'm assuming there are scenarios where using these keywords is either simpler or is the only way to accomplish the required result.

What I can't find is the scenarios....

Can anyone fill in the gap? I don't need entire sets of SQL. I just need a starting point to play with i.e. a brief description of a scenario or two... I can expand on that myself.


回答1:


Look at this post

One of the benefits of references is that it can be thought as a ‘lightweight’ entity in which we don’t need to spend resources in creating and maintaining the full entity state/values until it is really necessary. Once you have a ref to an entity, you can dereference it by using DEREF expression or by just invoking a property of the entity




回答2:


TL;DR - REF/DEREF are similar to C++ pointers. It they are references to persisted entities (not entities which have not be saved to a data source).

Why would you use such a thing?: A reference to an entity uses less memory than having the DEFEF'ed (or expanded; or filled; or instantiated) entity. This may come in handy if you have a bunch of records that have image information and image data (4GB Files stored in the database). If you didn't use a REF, and you pulled back 10 of these entities just to get the image meta-data, then you'd quickly fill up your memory.

I know, I know. It'd be easier just to pull back the metadata in your query, but then you lose the point of what REF is good for :-D



来源:https://stackoverflow.com/questions/9556589/in-what-scenarios-would-i-need-to-use-the-createref-deref-and-ref-keywords

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!