appengine equivalent of a "NOT IN" query - google-app-engine

I am coming to Appengine from a relational database background, and was considering how best to accomplish this task.
Basically I have a table of objects and would like to retrieve a pair that a user has never seen.
In mysql for example the most straightforward could be something like
SELECT *
FROM object_pairs
WHERE id NOT IN(
SELECT object_pair_id
FROM user_object_pairs
WHERE user_id = :user_id
)
Ideally I'd also like to be able to retrieve a random (or semi-random) pair from the possible results.
Any suggestions appreciated.
Thanks

There's no easy way to do this. The SQL query you suggest will be executed by most SQL databases as first constructing an in-memory list of IDs, then doing a linear scan over the table, returning only rows that aren't in the list of IDs. You can do the same in App Engine: get the user's 'seen list', do a query for all entities, and skip those that he's seen before.

Related

Alias in where query in SOQL

I am trying to take the count from the object Lead using SOQL.
When I am hitting the below mention query, I am getting the results.
select 'Lead' as source_table,count(*) as source_count from Lead
But when I am trying to give where condition with this query, then it is throwing me the error.
select 'Lead' as source_table,count() as source_count from Lead where
CreatedDate > 2020-02-24T09:43:51Z
Is there anything that I am missing.
Your query is not a valid SOQL, where exactly are you doing it? In real Salesforce (Apex, developer console, Salesforce APIs) or do you work on some copy of data imported to MSSQL for example?
Even your basic form (select 'Lead' as source_table,count(*) as source_count from Lead) will not parse OK in SOQL. There's no AS keyword and no dummy columns.
Closest would be
SELECT COUNT(Id) source_count
FROM Lead
WHERE CreatedDate > 2020-02-24T09:43:51Z
There will be no artificial column with "Lead" in it. If you need some key-value thing across multiple database tables you'd have to process results in Apex, maybe make a helper class to store results, maybe a Map<String, Integer> would be enough. But if you're doing it as API access then much better idea would be to access the dedicated APIs
getUpdated REST API - has "updated" and "deleted" versions, if you really need "created" it's not that great unless you filter them out later somehow
record count across multiple objects - but it's really count in table, similar to Setup -> Storage Usage. No way to pass a WHERE clause
ask your SF admin to build a report that does what you need (you don't need details, just a count, right?), then access the results with Analytics API. Or even build the report on the fly: example

SQL query getting data from 2 different database objects

I am very much a beginner at more in depth SQL queries other then simple queries needed to more manage the server side then the database side so any help here is greatly appreciated.
I have a a database for a ticketing system where I need to take data from two different objects and combine the results into the results.
Looks something like this:
+Prof1
- Columns
*AssignedTech (tech assigned task)
*Matters (unique matter ID)
*Type (open, closed, development etc.)
+Matters
- Columns
*MatterNumber
What I am trying to do is get the Matter Number into the query like below but don't know other then perhaps dumping the results to Excel and filtering them from there to get that data into this query. Matters in the DBO corresponds to the unique Matter ID noted above. If i could even run the query below and then use those results to query against the Matters DBO to get the matter ID along with it.
select *
from AssignedTech
where Type like 'open%'
order by Matters
I Believe you want to do is a SQL Join! (I suppose that the Column Matters at Prof1 Table is a reference to the MatterNumber at Matters Table)...
So, to do the join, you must write a query like this:
SELECT A.*, B.MatterNumber
FROM Prof1 A, Matters B
WHERE A.Matters = B.MatterNumber AND A.Type like 'open%'
ORDER BY A.Matters;
Hope this help (also, that I understand you correctly)
Mureinik, I wish I could post a screenshot but hopefully this helps. The section of the DB Basically looks something like this
Example

is it feasible to add multiple index parameters in , Full text search API ? google app engine

I need to run a search query like this : SELECT from results WHERE owner=mike,john,tom ...etc which should provide a concatenation of the items owned by those usernames. There may be about 100-200 usernames in the query. Would be feasible to do this using the full text search API (I already use it for keyword queries) using the usernames as filters (e.g. filter(1)=mike&filter(2)=john etc ) or I should try some kind of datastore join operation ?
You could construct a query like this:
owner:mike OR owner:john OR owner:tom etc., but this will not be efficient for a large number of possible owner values.
Instead, consider whether you could group the owners according to some application semantics (e.g. some owners in 'usergroup1', some in 'usergroup2', etc.), and then instead query for documents with e.g. usergroup:usergroup1.
Alternately, if you could assign each owner a numeric value (that makes sense for your application), you can use numeric comparators, e.g. owner_number < 10.
I guess first query is MySQL query, right?
If field 'owner' have index in MySQL query then you could use following structure:
SELECT from results WHERE owner IN ('mike','john','tom')
I can't say too much about APP Engine, but certainly you need to use filters.

SQL server search

I'm going to perform a search in my SQL server DB (ASP.NET, VS2010,C#), user types a phrase and I should search this phrase in several fields, how is it possible? do we have functions such as CONTAINS() in SQL server? can I perform my search using normal queries or I should work in my queries using C# functions?
for instance I have 3 fields in my table which can contain user search phrase, is it OK to write following sql command? (for instance user search phrase is GAME)
select * from myTable where columnA='GAME' or columnB='GAME' or columnC='GAME
I have used AND between different conditions, but can I use OR? how can I search inside my table fields? if one of my fields contains the phrase GAME, how can I find it? columnA='GAME' finds only those fields that are exactly 'GAME', is it right?
I'm a bit confused about my search approach, please help me, thanks guys
OR works fine if you want at least one of the conditions to be true.
If you want to search inside your text strings you can use LIKE
select * from myTable where columnA like '%GAME%' or columnB like '%GAME%' or columnC like '%GAME%'
Note that % is the wildcard.
If you want to find everything that begins with 'GAME' you type LIKE 'GAME%', if you allow 'GAME' to be in the middle you need % in both ends.
You can use LIKE instead of equals and then it can contain wildcard characters, so your example could be:
select * from myTable where columnA LIKE '%GAME%' or columnB LIKE '%GAME%' or columnC LIKE '%GAME%'
Further information may be found in MSDN
This is going to do some pretty heavy lifting in terms of what the database has to do though - I would suggest you consider something like full text search as I think it would more likely be suited to your scenario and provide faster results (of course, if you never have many records to search LIKE would probably do fine). Information on this is also in MSDN
Don't use LIKE, as suggested by other answers. It won't work with indexes, and therefore will be slow to return and expensive to run. Instead, you have two options:
Option 1: Full-Text Indexes
do we have functions such as CONTAINS() in SQL server?
Yes! You can use the CONTAINS() function in sql server. You just have to set up a full-text index for each of the columns you need to search on.
Option 2: Lucene.Net
Lucene.Net is a popular client-side library for searching text data that integrates closely with Sql Server. You can use it to make implementing your search a little easier.

Querying XML columns in SQLServer 2005

There is a field in my company's "Contacts" table. In that table, there is an XML type column. The column holds misc data about a particular contact. EG.
<contact>
<refno>123456</refno>
<special>a piece of custom data</special>
</contact>
The tags below contact can be different for each contact, and I must query these fragments
alongside the relational data columns in the same table.
I have used constructions like:
SELECT c.id AS ContactID,c.ContactName as ForeName,
c.xmlvaluesn.value('(contact/Ref)[1]', 'VARCHAR(40)') as ref,
INNER JOIN ParticipantContactMap pcm ON c.id=pcm.contactid
AND pcm.participantid=2140
WHERE xmlvaluesn.exist('/contact[Ref = "118985"]') = 1
This method works ok but, it takes a while for the Server to respond.
I have also investigated using the nodes() function to parse the XML nodes and exist() to test if a nodes holds the value I'm searching for.
Does anyone know a better way to query XML columns??
If you are doing one write and a lot of reads, take the parsing hit at write time, and get that data into some format that is more query-able. A first suggestion would be to parse them into a related but separate table, with name/value/contactID columns.
I've found the msdn xml best practices helpful for working with xml blob columns, might provide some inspiration...
http://msdn.microsoft.com/en-us/library/ms345115.aspx#sql25xmlbp_topic4
In addition to the page mentioned by #pauljette, this page has good performance optimization advice:
http://msdn.microsoft.com/en-us/library/ms345118.aspx
There's a lot you can do to speed up the performance of XML queries, but it will never be as good as properly indexed relational data. If you are selecting one document and then querying inside just that one, you can do pretty well, but when your query needs to scan through a bunch of similar documents looking for something, it's sort of like a key lookup in a relational query plan (that is, slow).
If you have a XSD for your Xml then you can import that into your database and you can then build indexes for your Xml data.
Try this
SELECT * FROM conversionupdatelog WHERE
convert(XML,colName).value('(/leads/lead/#LeadID=''xyz#airproducts.com'')[1]', 'varchar(max)')='true'

Resources