There are 1 million entries in a mysql table [name VARCHAR, id bigint(20), category int(11)]
I use the following SELECT SQL in mybatis select xml
<select id="getIdNameMap" parameterType="String" resultType="java.util.HashMap">
select NAME , ID from PRODUCTS where CATEGORY = #{value}
</select>
I use the following java code
Reader reader = Resources.getResourceAsReader(SQL_MAP_CONFIG_FILE_PATH);
sqlMapClient = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlMapClient.openSession(true);
Map kvpList = sqlSession.selectMap("getIdNameMap", "1", "NAME");
It takes 6902 secs to pull data into the map
With the following code it takes 2368 Secs to pull the same data into a map
ResultSet rs = sqlSession.getConnection().createStatement().executeQuery("select NAME , ID from PRODUCTS where CATEGORY = 1");
while (rs.next()){
map.put(rs.getString(1), Long.parseLong(rs.getString(2)));
}
System.out.println(map.size());
rs.close();
Any suggestions?
Related
I read most of the solutions here with similar questions and it did not solve my problem and I cannot find anything online that can help me.
I am trying to make query on condition where user_id = session user_id but I get error when I make INNER join
ambiguous column name
for this
public List<CartModelClass>getCarts1(){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = { "ID" , "user_id", "food_id", "quantity", "price", "origin", "destination","description","company_name","search_id"};
String sqltable2 = "OrderDetails LEFT JOIN OrderDetails WHERE user_id LIKE '%%' ";
qb.setTables(sqltable2);
Cursor c = qb.query(db,sqlSelect, null, null ,null ,null ,null);
final List<CartModelClass> result = new ArrayList<>();
if (c.moveToFirst()) {
do {
result.add(new CartModelClass(
c.getString(c.getColumnIndex("user_id")),
c.getString(c.getColumnIndex("food_id")),
c.getString(c.getColumnIndex("quantity")),
c.getString(c.getColumnIndex("price")),
c.getString(c.getColumnIndex("origin")),
c.getString(c.getColumnIndex("destination")),
c.getString(c.getColumnIndex("description")),
c.getString(c.getColumnIndex("company_name")),
c.getString(c.getColumnIndex("search_id"))
));
} while (c.moveToNext());
}
return result;
}
so I changed InnerJoin and made it just table where user_id like"%%" but I only get the last user_id who added to cart and show all data for all users
I want to show only added cart for user_id = session user_id so i can use it in here
loadListFood
private void loadListFood(){
sessionManager= new SessionManager(getActivity());
final Hashmap<String, String> user = sessionManager.getUserDetail();
user.get(USER_ID);
listdata = new Database(this.getContext.getCarts1());
for(CartModelClass order : listdata)
user_id = order.getUser_id
if(user.get(USER_ID).equals(user_id)){
listdata = new Database(this.getContext()).getCarts();
adapter = new CartAdapter(listdata, this.getContext());
recyclerView.setAdapter(adapter);
int total = 0;
for (CartModelClass order : listdata) {
total += (Integer.parseInt(order.getPrice())) * (Integer.parseInt(order.getQuantity()));
Locale locale = new Locale("en", "US");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
txtTotalPrice.setText(fmt.format(total));
}
}else {
Toast.makeText(getContext(), "No Cart Added", Toast.LENGTH_LONG).show();
}
}
You are self joining the table OrderDetails.
In this case you must set aliases to both copies of the table, like:
OrderDetails as o1 LEFT JOIN OrderDetails as o2 ...
Now in the ON clause you must qualify the column names properly, like:
ON o1.user_id = o2.something
If you don't, you get that error message, because the column name user_id could belong to either of the 2 copies of the table.
Also:
What is session user_id? Is it a column name?
If it is then the problem is that it contains a space in its name.
Enclose it in square brackets, so the statemnet should be:
OrderDetails as o1 LEFT JOIN OrderDetails as o2
ON o1.user_id = o2.[session user_id]
I want to fetch metadata information regarding a Database view.
I can find my view using the following code
DatabaseMetaData databaseMetaData = connection.getMetaData();
String tableName = "";
ResultSet rSet = databaseMetaData.getTables(null, databaseName, null, new String[] {TABLE, VIEW});
while (rSet.next()) {
tableName = rSet.getString(TABLE_NAME_COLUMN);
if (rSet.getString(TABLE_TYPE_COLUMN).equals(TABLE)) {
tableNames.add(tableName);
} else if (rSet.getString(TABLE_TYPE_COLUMN).equals(VIEW)) {
viewNames.add(tableName);
}
}
Now when i have the view name, i want to fetch the list of data columns in the VIEW.
I use the following code
ResultSet rSet = databaseMetaData.getColumns(null, databaseName, table, null);
while (rSet.next()) {
columnDefinitions.add(new ColumnDefinition(rSet.getString(COLUMN_NAME_COLUMN), getColumnClassName(rSet
.getInt(DATA_TYPE_COLUMN))));
}
the concern here being that this query only returns the list of base columns from which this VIEW was derived. Say if my base table had 3 columns and i created a view if that table with 2 extra columns deriving information from the 3 base columns.
I want to fetch all 5 columns name and information. Is this possible ?
Is there any other method available to do this ?
I'm facing an issue to select the value from the same object. I provided the query below.
I'm migrating a Java J2EE application to Salesforce, the below query works in my SQL.
I'm trying to do the same in SOQL, but it doesn't work.
SELECT DATA1__c, TEXT__c
FROM PARAMETERS__c
WHERE ( (TYPE__c = 'ADMINISTRATEUR')
AND (KEY1__c LIKE 'MONTAGE%') (AND KEY2__c = ''))
AND (DATA1__c
IN (SELECT KEY1__c
FROM Parameters__c
WHERE TYPE__c = 'PERE_TECHNIQUE'))
In the above query I need to take the value where TYPE is based on 'TECHNIQUE' where KEY1__c should be matched to DATA1__c from the outer query.
The query is very similar to this example
SELECT Id
FROM Idea
WHERE ((Idea.Title LIKE 'Vacation%')
AND (CreatedDate > YESTERDAY)
AND (Id IN (SELECT ParentId
FROM Vote
WHERE CreatedById = '005x0000000sMgYAAU'))
The only difference is that IN clause is used with a different object.
In my query I'm trying to use IN clause from the same object parameters.
Kindly let me know in case of any further clarifications.
try the following
List<String> pereTechniqueParams = new List<String>();
for (String key:
[SELECT KEY1__c FROM Parameters__c WHERE TYPE__c = 'PERE_TECHNIQUE']) {
pereTechniqueParams.add(key.KEY1__c);
}
List<Parameters__c> params = [SELECT DATA1__c, TEXT__c
FROM PARAMETERS__c
WHERE (TYPE__c = 'ADMINISTRATEUR'
AND KEY1__c LIKE 'MONTAGE%'
AND KEY2__c = '')
AND DATA1__c IN:pereTechniqueParams];
UPDATE:
for (Parameters__c key1 : [SELECT KEY1__c
FROM Parameters__c WHERE TYPE__c = 'PERE_TECHNIQUE']) {
pereTechniqueParams.add(key1.KEY1__c);
}
Don't use String use Parameters__c
public class LookUpController {
public List<Parameters__c> getParamters() {
List<String> pereTechniqueParams = new List<String>();
for (Parameters__c key1 : [SELECT KEY1__c
FROM Parameters__c WHERE TYPE__c = 'PERE_TECHNIQUE']) {
pereTechniqueParams.add(key1.KEY1__c);
}
List<Parameters__c> params = [SELECT DATA1__c, TEXT__c
FROM PARAMETERS__c
WHERE TYPE__c = 'ADMINISTRATEUR'
AND KEY1__c LIKE 'MONTAGE%'
AND KEY2__c = ''
AND Data1__c IN: pereTechniqueParams];
return params;
}
}
The Vote Id and Idea Id is not same, The inner selection return list of Vote, And there is not result for sub-query Id IN (SELECT ParentId FROM Vote....
Channge Code To
set<Id> ideaIdSet = new set<Id>();
for(Vote vote : [SELECT ParentId FROM Vote WHERE CreatedById = '005x0000000sMgYAAU']){
ideaIdSet.add(vote.ParentId);
}
SELECT Id
FROM Idea
WHERE ((Title LIKE 'Vacation%')
AND (CreatedDate > YESTERDAY)
AND (Id IN ideaIdSet)
Im new to Linq and Im sure that I have gone about this in a convoluted manner. Im trying to do something like this SQL in Linq:
SELECT DISTINCT
count(vendor) as vendorCount,
reqDate,
status,
openDate,
item,
poDate,
count(responseDate) as responseCount
FROM
myTable
GROUP BY
reqDate, status, openDate, item, poDate
HAVING
reqDate > openDate
Here is what I have so far.
var groupQuery = (from table in dt.AsEnumerable()
group table by new
{
vendor = table["vendor"], reqdate = table.Field<DateTime>("ReqDate"), status = table["status"],
open = table["openDate"],
item = table["item"),
podate = table.Field<DateTime>("PODate"), responsedate = table.Field<DateTime>("responseDate"),
}
into groupedTable
where Having(groupedTable.Key.reqdate, groupedTable.Key.openDate) == 1
select new
{
x = groupedTable.Key,
y = groupedTable.Count()
}).Distinct();
foreach (var req in groupQuery)
{
Console.WriteLine("cols: {0} count: {1} ",
req.x, req.y);
}
The Having() is a function that takes two datetime parameters and returns a 1 if the reqDate is greater than the openDate. It compiles and runs, but it obviously does not give me the results I want. Is this possible using Linq? I want to push this data to an excel spreadsheet so Im hoping to create a datatable from this linq query. Would I be better off just creating a dataview from my datatable and not mess with Linq?
The SQL code is grouping by only some of the fields, while your LINQ statement is grouping by all of the fields, so the only items that would get grouped would be duplicates. If you group by only the fields that the SQL query groups by, you should get the correct answer. Your Having() method words fine, but is not necessary and is less readable.
var groupQuery = (from table in dt.AsEnumerable()
group table by new
{
reqdate = table.Field<DateTime>("ReqDate"),
status = table["status"],
open = table["openDate"],
item = table["item"),
podate = table.Field<DateTime>("PODate")
}
into groupedTable
where groupedTable.Key.reqdate > groupedTable.Key.openDate
select new
{
x = groupedTable.Key,
VenderCount = groupedTable.Select(t => t["vendor"])
.Distinct()
.Count(),
ResponseCount = groupedTable.Select(t => t.Field<DateTime>("responseDate"))
.Distinct()
.Count()
}).Distinct();
We are trying to upgrade to NHibernate 3.0 and now i am having problem with the following Linq query. It returns "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS." error.
This is the linq query in the controller.
var list = (from item in ItemTasks.FindTabbedOrDefault(tab)
select new ItemSummary
{
Id = item.Id,
LastModifyDate = item.LastModifyDate,
Tags = (from tag in item.Tags
select new TagSummary
{
ItemsCount = tag.Items.Count,
Name = tag.Name
}).ToList(),
Title = item.Title
});
and the following is the sql generated for this query
select TOP ( 1 /* #p0 */ ) item0_.Id as col_0_0_,
item0_.LastModifyDate as col_1_0_,
(select (select cast(count(* ) as INT)
from dbo.ItemsToTags items3_,
dbo.Item item4_
where tag2_.Id = items3_.Tag_id
and items3_.Item_id = item4_.Id),
tag2_.Name
from dbo.ItemsToTags tags1_,
dbo.Tag tag2_
where item0_.Id = tags1_.Item_id
and tags1_.Tag_id = tag2_.Id) as col_2_0_,
item0_.Title as col_3_0_ from dbo.Item item0_ order by item0_.ItemPostDate desc
ps:If i remove the Tags property in the linq query, it works fine.
Where is the problem in the query?
Thanks in advance.
I've got the same Generic ADO Exception error, I think it's actually the limitation of SQL server;
Is it possible somehow load object graph with projections in collections?
If I try this one:
var cats = q.Select(t => new cat()
{
NickName = t.NickName,
Legs = t.Legs.Select(l => new Leg()
{
Color = l.Color,
Size = l.Size
}).ToList()
}).ToList();
That does the same error..