Search Results

Search found 8344 results on 334 pages for 'count'.

Page 7/334 | < Previous Page | 3 4 5 6 7 8 9 10 11 12 13 14  | Next Page >

  • Postgresql count+sort performance

    - by invictus
    I have built a small inventory system using postgresql and psycopg2. Everything works great, except, when I want to create aggregated summaries/reports of the content, I get really bad performance due to count()'ing and sorting. The DB schema is as follows: CREATE TABLE hosts ( id SERIAL PRIMARY KEY, name VARCHAR(255) ); CREATE TABLE items ( id SERIAL PRIMARY KEY, description TEXT ); CREATE TABLE host_item ( id SERIAL PRIMARY KEY, host INTEGER REFERENCES hosts(id) ON DELETE CASCADE ON UPDATE CASCADE, item INTEGER REFERENCES items(id) ON DELETE CASCADE ON UPDATE CASCADE ); There are some other fields as well, but those are not relevant. I want to extract 2 different reports: - List of all hosts with the number of items per, ordered from highest to lowest count - List of all items with the number of hosts per, ordered from highest to lowest count I have used 2 queries for the purpose: Items with host count: SELECT i.id, i.description, COUNT(hi.id) AS count FROM items AS i LEFT JOIN host_item AS hi ON (i.id=hi.item) GROUP BY i.id ORDER BY count DESC LIMIT 10; Hosts with item count: SELECT h.id, h.name, COUNT(hi.id) AS count FROM hosts AS h LEFT JOIN host_item AS hi ON (h.id=hi.host) GROUP BY h.id ORDER BY count DESC LIMIT 10; Problem is: the queries runs for 5-6 seconds before returning any data. As this is a web based application, 6 seconds are just not acceptable. The database is heavily populated with approximately 50k hosts, 1000 items and 400 000 host/items relations, and will likely increase significantly when (or perhaps if) the application will be used. After playing around, I found that by removing the "ORDER BY count DESC" part, both queries would execute instantly without any delay whatsoever (less than 20ms to finish the queries). Is there any way I can optimize these queries so that I can get the result sorted without the delay? I was trying different indexes, but seeing as the count is computed it is possible to utilize an index for this. I have read that count()'ing in postgresql is slow, but its the sorting that are causing me problems... My current workaround is to run the queries above as an hourly job, putting the result into a new table with an index on the count column for quick lookup. I use Postgresql 9.2.

    Read the article

  • get count from Iqueryable<T> in linq-to-sql?

    - by Pandiya Chendur
    The following code doesn't seem to get the correct count..... var materials = consRepository.FindAllMaterials().AsQueryable(); int count = materials.Count(); Is it the way to do it.... Here is my repository which fetches records... public IQueryable<MaterialsObj> FindAllMaterials() { var materials = from m in db.Materials join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id where m.Is_Deleted == 0 select new MaterialsObj() { Id = Convert.ToInt64(m.Mat_id), Mat_Name = m.Mat_Name, Mes_Name = Mt.Name, }; return materials; }

    Read the article

  • How to count the letters in a text with Javascript?

    - by Doguhanca
    I am currently trying to write a ''web application'' that has a simple text area inside, in which I want the letters of the text written to be pointed out. For example, if I write: ''How old are you? I am 19 years old'' I need a code to tell me how many 'A's and 'Y's and 'D's (and all letters of the alphabet from 0-26) are used in this sentence when I press a button on a HTML/ CSS page. Could you please tell me what I must write into my .JS file and what I should write into my .HTML file to do this with a click of a button when something is written in the ? I hope my explanation was detailed enough. Thanks! Edit (I'm very sorry for the problems I caused) - What I have done so far looks like this: HTML: <link rel="stylesheet" type="text/css" href="theme.css"> <meta charset="utf-8"> <script src="test.js" type="text/javascript"></script> <div class='header'> Al.Fa.Be </div> <div class='yaz'> <textarea></textarea> </div> <div class='description'> <a href='http://www.google.com'>Ara</a> </div> <div class='description2'> <input id="clickMe" type="button" value="Hesapla" onclick="doFunction();" /> </div> CSS: body{ background:white; } selection{ background:#CCC; } #clickMe{ background:#CCC; border:1px solid #333; } .header{ font-size:70px; font-weight:bold; font-family:Arial; color:#333; margin-left:580px; padding-top:200px; } textarea{ width:1210px; height:40px; color:black; margin-top:20px; margin-left:100px; padding-left:10px; padding-top:10px; font-size:18px; font-family:Arial; } .description{ background:#f2f2f2; padding:6px; width:50px; text-align:center; border:1px solid #ddd; font-family:Arial; margin-left:620px; margin-top:20px; font-size:14px; } .description a{ color:#555; text-decoration:none; } .description2{ background:#f2f2f2; padding:6px; width:60px; text-align:center; border:1px solid #ddd; font-family:Arial; margin-left:750px; margin-top:-30px; font-size:14px; } .description2 a{ color:#555; text-decoration:none; } .yaz{ color:white; } Javascript: // Input name. Count number of alphabets a-z class program                                                          {     public static void main(String[] args)     {         String name = args[0];         int count[] = new int[29];         int i,p;         int n = name.length();         name = name.toUpperCase();         char c;         for (i=0; i<29; i++)         {             count[i] = 0;           }         for (i=0; i<n; i++)         {             c = name.charAt(i);             p = (int) c;             count[p-65]++;         }         for (i=0; i<29 ; i++)         {             if (count[i] >0)             {                 System.out.println((char)(i+65) + " occurs " + count[i] + " times");             }         }     } }

    Read the article

  • How can I count existing and non-existing values with MySQL?

    - by jaya malladi
    I am new to MySQL. I have a table with answer ids. Answers can look like this:a1, a2, a3 ..., but due to some problems some are NULL, some are blank, and some are others like 1 a etc. Now I want to calculate the count of ids with a1 a2 a3 distinctly. But how is it possible to do this leaving others like NULL, blanks and garbage. The output should look like this atype count a1 45 a2 0 a3 56 If there is no row entry for a particular answer, the count should be 0.

    Read the article

  • Android-SQLite: How to Count specific value from Column?

    - by sanpatil
    I have two table (TABLE_EXAM,TABLE_RESULT). Here is value of my TABLE_RESULT. result_id exam_id question_id correct_answer 1 2 4 y 2 2 5 y 3 2 6 n 4 2 7 y I need to count how many correct_answer='y' where exam_id=2. I try following code but it return 0. public int calculateResult(int examId,String confirmAnswer) { int correctAnswer=0; try { SQLiteDatabase db=this.getWritableDatabase(); String selectQuery=("select count(correctAnswer) from result where exam_id ='" + examId + "' and correctAnswer ='" + 'y' +"'" ); // String selectQuery=("SELECT COUNT(*)FROM result WHERE exam_id ='" + examId + "' and correctAnswer ='" + confirmAnswer +"'" ); Cursor cursor = db.rawQuery(selectQuery, null); if(cursor.moveToLast()) { correctAnswer=cursor.getInt(3); } } catch(Exception e) { e.printStackTrace(); } return correctAnswer; } In variable confirm_answer i pass "y". Give me some hint or reference. Any help is appreciated. Thanks in Advance

    Read the article

  • Count function on tree structure (non-binary)

    - by Spevy
    I am implementing a tree Data structure in c# based (largely on Dan Vanderboom's Generic implementation). I am now considering approach on handling a Count property which Dan does not implement. The obvious and easy way would be to use a recursive call which Traverses the tree happily adding up nodes (or iteratively traversing the tree with a Queue and counting nodes if you prefer). It just seems expensive. (I also may want to lazy load some of my nodes down the road). I could maintain a count at the root node. All children would traverse up to and/or hold a reference to the root, and update a internally settable count property on changes. This would push the iteration problem to when ever I want to break off a branch or clear all children below a given node. Generally less expensive, and puts the heavy lifting what I think will be less frequently called functions. Seems a little brute force, and that usually means exception cases I haven't thought of yet, or bugs if you prefer. Does anyone have an example of an implementation which keeps a count for an Unbalanced and/or non-binary tree structure rather than counting on the fly? Don't worry about the lazy load, or language. I am sure I can adjust the example to fit my specific needs. EDIT: I am curious about an example, rather than instructions or discussion. I know this is not technically difficult...

    Read the article

  • How do I count how many arrays have the same name within a multidimensional array with php?

    - by zeckdude
    I have a multidimensional array, and I would have multiple arrays within it. Some of those arrays contain multiple arrays within them as well, and I would like to count how many arrays are within the second array(the date). This is an example of the structure of the multidimensional array: $_SESSION['final_shipping'][04/03/2010][book] $_SESSION['final_shipping'][04/12/2010][magazine] $_SESSION['final_shipping'][04/12/2010][cd] This is the foreach statement I am currently using to count how many of the second array(the one with the dates) exists. foreach($_SESSION['final_shipping'] as $date_key => $date_value) { foreach ($date_value as $product_key => $product_value) { echo 'There are ' . count($date_key) . ' of the ' . $date_key . ' selection.<br/>'; } } It is currently outputting this: There are 1 of the 04/03/2010 selection. There are 1 of the 04/12/2010 selection. There are 1 of the 04/12/2010 selection. I would like it to output this: There are 1 of the 04/03/2010 selection. There are 2 of the 04/12/2010 selection.

    Read the article

  • NASM: Count how many bits in a 32 Bit number are set to 1.

    - by citronas
    I have a 32 Bit number and want to count know how many bits are 1. I'm thinking of this pseudocode: mov eax, [number] while(eax != 0) { div eax, 2 if(edx == 1) { ecx++; } shr eax, 1 } Is there a more efficient way? I'm using NASM on a x86 processor. (I'm just beginning with assembler, so please do not tell me to use code from extern libraries, because I do not even know how to include them ;) ) (I just found http://stackoverflow.com/questions/109023/best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer which also contains my solution. There are other solutions posted, but unfortunatly I can't seem to figure out, how I would write them in assembler)

    Read the article

  • MySQL Query, how to group and count in one row ?

    - by Akarun
    Hi All, To simplify, I have tree tables: products, products-vs-orders, orders products fields : 'ProductID', 'Name', 'isGratis', ... products-vs-orders fields : 'ProductID', 'OrderID' orders fields : 'OrderID', 'Title', ... Actually, I have a query like this: SELECT orders.OrderID, orders.Title, COUNT(`products`.`isGratis`) AS "Quantity", `products`.`isGratis` FROM `orders`, `products-vs-orders`, `products` WHERE `orders`.`OrderID` = `products-vs-orders`.`OrderID` AND `products-vs-orders`.`ProductID` = `products`.`ProductID` GROUP BY `products`.`PackID`, `products`.`isGratis` This query works and return this surch of result: OrderID, Title, Quantity, isGratis 1 My Order 20 0 1 My Order 3 1 2 An other 8 0 2 An other 1 1 How can I retrieve the count of products 'gratis' and 'paid' in to separate cols ? OrderID, Title, Qt Paid, Qt Gratis 1 My Order 20 3 2 An other 8 1 Thanks for your help

    Read the article

  • count(*) vs count(row-name) - which is more correct?

    - by bread
    Does it make a difference if you do count(*) vs count(row-name) as in these two examples? I have a tendency to always write count(*) because it seems to fit better in my mind with the notion of it being an aggregate function, if that makes sense. But I'm not sure if it's technically best as I tend to see example code written without the * more often than not. count(*): select customerid, count(*), sum(price) from items_ordered group by customerid having count(*) > 1; vs. count(row-name): SELECT customerid, count(customerid), sum(price) FROM items_ordered GROUP BY customerid HAVING count(customerid) > 1;

    Read the article

  • count(*) vs count(column-name) - which is more correct?

    - by bread
    Does it make a difference if you do count(*) vs count(column-name) as in these two examples? I have a tendency to always write count(*) because it seems to fit better in my mind with the notion of it being an aggregate function, if that makes sense. But I'm not sure if it's technically best as I tend to see example code written without the * more often than not. count(*): select customerid, count(*), sum(price) from items_ordered group by customerid having count(*) > 1; vs. count(column-name): SELECT customerid, count(customerid), sum(price) FROM items_ordered GROUP BY customerid HAVING count(customerid) > 1;

    Read the article

  • Facebook Share Button and Counter no longer displaying any Count

    - by donaldthe
    Is it just me or did the Facebook Share button that displays the count of shares and likes just stop working over the past few days? The sharing still works, the count of shares no longer displays. The link that is generated look like this http://www.facebook.com/sharer.php?u= and the JavaScript file on my page is this http://static.ak.fbcdn.net/connect.php/js/FB.Share I haven't changed anything and this has worked for years

    Read the article

  • Row Count Plus Transformation

    As the name suggests we have taken the current Row Count Transform that is provided by Microsoft in the Integration Services toolbox and we have recreated the functionality and extended upon it. There are two things about the current version that we thought could do with cleaning up Lack of a custom UI You have to type the variable name yourself In the Row Count Plus Transformation we solve these issues for you.

    Read the article

  • SQL SERVER – Find Max Worker Count using DMV – 32 Bit and 64 Bit

    - by pinaldave
    During several recent training courses, I found it very interesting that Worker Thread is not quite known to everyone despite the fact that it is a very important feature. At some point in the discussion, one of the attendees mentioned that we can double the Worker Thread if we double the CPU (add the same number of CPU that we have on current system). The same discussion has triggered this quick article. Here is the DMV which can be used to find out Max Worker Count SELECT max_workers_count FROM sys.dm_os_sys_info Let us run the above query on my system and find the results. As my system is 32 bit and I have two CPU, the Max Worker Count is displayed as 512. To address the previous discussion, adding more CPU does not necessarily double the Worker Count. In fact, the logic behind this simple principle is as follows: For x86 (32-bit) upto 4 logical processors  max worker threads = 256 For x86 (32-bit) more than 4 logical processors  max worker threads = 256 + ((# Procs – 4) * 8) For x64 (64-bit) upto 4 logical processors  max worker threads = 512 For x64 (64-bit) more than 4 logical processors  max worker threads = 512+ ((# Procs – 4) * 8) In addition to this, you can configure the Max Worker Thread by using SSMS. Go to Server Node >> Right Click and Select Property >> Select Process and modify setting under Worker Threads. According to Book On Line, the default Worker Thread settings are appropriate for most of the systems. Reference: Pinal Dave (http://blog.SQLAuthority.com) Filed under: Pinal Dave, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL System Table, SQL Tips and Tricks, T SQL, Technology Tagged: SQL DMV

    Read the article

  • How to count differences between two files on linux?

    - by Zsolt Botykai
    Hi all, I need to work with large files and must find differences between two. And I don't need the different bits, but the number of differences. For the differ rows I come up with diff --suppress-common-lines --speed-large-files -y File1 File2 | wc -l And it works, but is there a better way to do it? And how to count the exact number of differences (with standard tools like bash, diff, awk, sed some old version of perl)? Thanks in advance

    Read the article

  • sql query - how to count values in a row separately?

    - by n00b0101
    I have a table that looks something like this: id | firstperson | secondperson 1 | jane doe | 2 | bob smith | margie smith 3 | master shifu | madame shifu 4 | max maxwell | I'm trying to count all of the firstpersons + all of the secondpersons, if the secondpersons field isn't blank... Is there a way to do that?

    Read the article

  • Count times ID appears in a table and return in row.

    - by Tyler
    SELECT Boats.id, Boats.date, Boats.section, Boats.raft, river_company.company, river_section.section AS river FROM Boats INNER JOIN river_company ON Boats.raft = river_company.id INNER JOIN river_section ON Boats.section = river_section.id ORDER BY Boats.date DESC, river, river_company.company Returns everything I need. But how would I add a [Photos] table and count how many times Boats.id occurs in it and add that to the returned rows. So if there are 5 photos for boat #17 I want the record for boat #17 to say PhotoCount = 5

    Read the article

  • Count the number of times a string appears within a string...

    - by onekidney
    I simply have a string that looks something like this: "7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false" All I want to do is to count how many times the string "true" appears in that string. I'm feeling like the answer is something like String.CountAllTheTimesThisStringAppearsInThatString() but for some reason I just can't figure it out. Help?

    Read the article

  • How do I do a .count on the model an object belongs_to in rails?

    - by Angela
    I have @contacts_added defined as follows: @contacts_added = Contact.all(:conditions => ["date_entered >?", 5.days.ago.to_date]) Each contact belongs_to a Company. I want to be able the count the number of distinct Companies that @contacts_added belong to. contacts_added will have many contacts that belong to a single company, accessible through a virtual attribute contacts_added.company_name How do I do that?

    Read the article

  • [Linq to sql] query result what should i use Count() or Any()...

    - by Pandiya Chendur
    I am checking login of a user by this repository method, public bool getLoginStatus(string emailId, string password) { var query = from r in taxidb.Registrations where (r.EmailId == emailId && r.Password==password) select r; if (query.Count() != 0) { return true; } return false; } I saw in one of the previous questions !query.Any() would be faster... Which should i use? Any suggestion....

    Read the article

< Previous Page | 3 4 5 6 7 8 9 10 11 12 13 14  | Next Page >